Unity游戏开发:SQLite数据库在本地数据存储中的应用
发表时间: 2020-02-13 18:14
SQLiteDB sqLiteDb = new SQLiteDB(); string filename = Application.streamingAssetsPath + "/gameSql.db"; if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } sqLiteDb.Open(filename);
//id INTEGER PRIMARY KEY设置自增Id,DataKey BIGINT, Data BLOB,Size INT表示 字段名称 字段类型,字段名称 字段类型,字段名称 字段类型 string query_create = $"{CREATE TABLE IF NOT EXISTS}{表名}(id INTEGER PRIMARY KEY, DataKey BIGINT, Data BLOB,Size INT);"; SQLiteQuery query = new SQLiteQuery(sqLiteDb, password); query.Step(); query.Release(); query = new SQLiteQuery(sqLiteDb, queryCreate); query.Step(); query.Release();
string data = "需要存储的数据"; long id string queryInsert = $"INSERT INTO{tableName} (DataKey,Data) VALUES(?,?);"; SQLiteQuery query = new SQLiteQuery(sqLiteDb, password); query.Step(); query.Release(); query = new SQLiteQuery(sqLiteDb, queryInsert); query.Bind(id); query.Bind(data); query.Step(); query.Release(); netData.OnRecycling();
long id = 存储数据的key值 string data = "需要存储的数据"; string query_update = $"UPDATE {表名} SET Data = ? WHERE DataKey = ?;";// SQLiteQuery query = new SQLiteQuery(sqLiteDb, password); query.Step(); query.Release(); query = new SQLiteQuery(sqLiteDb, query_update); query.Bind(data); query.Bind(id); query.Step(); query.Release();
string value = string.Empty; long id = 存储数据的key值 //若删除 WHERE DataKey = ?将查询整张表内的数据 string query_select = $"SELECT * FROM {表名} WHERE DataKey = ?;"; SQLiteQuery query = new SQLiteQuery(sqLiteDb, password); query.Step(); query.Release(); query = new SQLiteQuery(sqLiteDb, query_select); query.Bind(id); if (query.Step()) { value = query.GetBlob("Data"); } query.Release();
long id = 存储数据的key值 //若删除 WHERE DataKey = ?将删除整张表内的数据 string query_delete = $"DELETE FROM {表名} WHERE DataKey = ?;"; SQLiteQuery query = new SQLiteQuery(sqLiteDb, password); query.Step(); query.Release(); query = new SQLiteQuery(sqLiteDb, query_delete); query.Bind(id); query.Step(); query.Release();
sqLiteDb.Close();
string outFile = string.Empty;#if UNITY_ANDROID outFile = Application.persistentDataPath + "/gameSql.db";#elif UNITY_IPHONE outFile = "file://" + Application.persistentDataPath + "/gameSql.db";#endif string resPath = Application.streamingAssetsPath + "/gameSql.db"; if (File.Exists(outFile)) File.Delete(outFile); UnityWebRequest request = UnityWebRequest.Get(resPath); request.timeout = 30; yield return request.SendWebRequest(); System.IO.File.WriteAllBytes(outFile,request.downloadHandler.data);