SQLite

作者: FrozenSunset | 来源:发表于2017-05-25 13:36 被阅读0次

    usingSystem.Collections;

    usingSystem.Collections.Generic;

    usingUnityEngine;

    usingMono.Data.Sqlite;

    publicclassSQLiteHelpScript{

    //定义数据库连接

    SqliteConnectionconnection;

    //定义SQL命令

    SqliteCommandcommand;

    //定义数据读取对象

    SqliteDataReaderreader;

    publicSQLiteHelpScript(stringdbPath){

    #ifUNITY_EDITOR

    stringconnectionString="DataSource="+dbPath;

    #elifUNITY_ANDROID

    string connectionString ="URL=file:"+ Application.persistentDataPath +"/PlayerData";

    #endif

    try{

    //创建数据库连接

    connection=newSqliteConnection(connectionString);

    connection.Open();

    }catch(SqliteExceptionex){

    Debug.Log(ex);

    }

    }

    //执行SQL命令

    publicSqliteDataReaderExecuteQuery(stringqueryString){

    command=connection.CreateCommand();

    command.CommandText=queryString;

    reader=command.ExecuteReader();

    returnreader;

    }

    //关闭数据库连接

    publicvoidCloseConnection(){

    //销毁Command

    if(command!=null){

    command.Dispose();

    }

    command=null;

    //销毁reader

    if(reader!=null){

    reader.Close();

    }

    reader=null;

    //销毁connection

    if(connection!=null){

    connection.Dispose();

    }

    connection=null;

    }

    //读取整张表

    publicSqliteDataReaderReadFullTable(stringtableName){

    stringquery="SELECT*FROM"+tableName;

    returnExecuteQuery(query);

    }

    //插入数据

    publicSqliteDataReaderInsertValues(stringtableName,string[]values){

    //获取数据表中的列数

    intfieldCount=ReadFullTable(tableName).FieldCount;

    //当前要插入的值的长度是否等于数据表中的列数

    if(values.Length!=fieldCount){

    //抛出异常

    thrownewSqliteException("values.Length!=fieldCount");

    }

    stringqueryString="INSERTINTO"+tableName+"VALUES("+values[0];

    for(inti=1;i

    queryString+=","+values[i];

    }

    queryString+=")";

    returnExecuteQuery(queryString);

    }

    publicSqliteDataReaderInsertValues(stringtableName,string[]colName,string[]values){

    if(values.Length!=colName.Length){

    thrownewSqliteException("values.Length!=colName.Length");

    }

    stringqueryString="INSERTINTO"+tableName+"("+colName[0];

    for(inti=1;i

    queryString+=","+colName[i];

    }

    queryString+=")"+"VALUES("+values[0];

    for(inti=1;i

    queryString+=","+values[i];

    }

    queryString+=")";

    returnExecuteQuery(queryString);

    }

    //更新数据

    publicSqliteDataReaderUpDateValues(stringtableName,string[]colName,string[]values,

    stringkey,stringoperation,stringvalue){

    //当列名的个数和值的个数不一致时抛出异常

    if(colName.Length!=values.Length){

    thrownewSqliteException("values.Length!=colName.Length");

    }

    stringqueryString="UPDATE"+tableName+"SET"+colName[0]+"="+values[0];

    for(inti=1;i

    queryString+=","+colName[i]+"="+values[i];

    }

    queryString+="WHERE"+key+operation+value;

    returnExecuteQuery(queryString);

    }

    //删除数据

    publicSqliteDataReaderDeleteValuesAnd(stringtableName,string[]colName,

    string[]values,string[]operation){

    if(colName.Length!=values.Length||colName.Length!=operation.Length||

    values.Length!=operation.Length){

    thrownewSqliteException("colName.Length!=values.Length!=operation.Length");

    }

    stringqueryString="DELETEFROM"+tableName+"WHERE"+colName[0]+operation[0]+values[0];

    for(inti=1;i

    queryString+="AND"+colName[i]+operation[i]+values[i];

    }

    returnExecuteQuery(queryString);

    }

    publicSqliteDataReaderDeleteValuesOr(stringtableName,string[]colName,string[]operation,

    string[]values){

    if(colName.Length!=values.Length||colName.Length!=operation.Length||

    values.Length!=operation.Length){

    thrownewSqliteException("colName.Length!=values.Length!=operation.Length");

    }

    stringqueryString="DELETEFROM"+tableName+"WHERE"+colName[0]+operation[0]+values[0];

    for(inti=1;i

    queryString+="OR"+colName[i]+operation[i]+values[i];

    }

    returnExecuteQuery(queryString);

    }

    //查找

    publicSqliteDataReaderSelectTable(stringtableName,string[]colName,string[]values,

    string[]operation,string[]keys){

    if(keys.Length!=values.Length||keys.Length!=operation.Length||

    values.Length!=operation.Length){

    thrownewSqliteException("colName.Length!=values.Length!=operation.Length");

    }

    stringqueryString="SELECT"+colName[0];

    for(inti=1;i

    queryString+=","+colName[i];

    }

    queryString+="FROM"+tableName+"WHERE"+keys[0]+operation[0]+values[0];

    for(intj=1;j

    queryString+="AND"+keys[j]+operation[j]+values[j];

    }

    returnExecuteQuery(queryString);

    }

    //创建表

    publicSqliteDataReaderCreatTable(stringtableName,string[]colName,string[]colTypes){

    if(colName.Length!=colTypes.Length){

    thrownewSqliteException("values.Length!=colName.Length");

    }

    stringqueryString="CREATETABLE"+tableName+"("+colName[0]+""+colTypes[0];

    for(inti=1;i

    queryString+=","+colName[i]+""+colTypes[i];

    }

    queryString+=")";

    Debug.Log(queryString);

    returnExecuteQuery(queryString);

    }

    }

    相关文章

      网友评论

          本文标题:SQLite

          本文链接:https://www.haomeiwen.com/subject/crxzxxtx.html