unity 2019 3.X版本使用sqlite
需要Mono.Data.Sqlite.dll,System.Data.dll,Sqlite3.dll3个dll.但是当前的版本不需要System.Data.dll。而且在当前版本的mono文件下找到的Mono.Data.Sqlite.dll,使用起来会报错。最后我去2018的unity安装目录下找了Mono.Data.Sqlite.dll(Editor\Data\Mono\lib\mono\2.0\ Mono.Data.Sqlite.dll)就不报错了。其他大神关于unity 使用sqlite教程。
sqlite.dll下载 x86 和 x64 架构
之后将dll放入到assets/pludins文件夹下。
建立数据库,我将数据库放在了streamingAssets下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | using UnityEngine; using System.Collections; using Mono.Data.Sqlite; using System; public class SQLite { public SqliteConnection connection; private SqliteCommand command; public SQLite( string path) { try { connection = new SqliteConnection(path); // 创建SQLite对象的同时,创建SqliteConnection对象 connection.Open(); // 打开数据库链接 Debug.Log( "Connect successful!" ); Command(); } catch (Exception ex) { Debug.Log(ex.Message); } } public SqliteCommand Command() { command = connection.CreateCommand(); return command; } // 【增加数据】 public SqliteDataReader InsertData( string table_name, string [] fieldNames, object [] values) { // 如果字段的个数,和数据的个数不相等,无法执行插入的语句,所以返回一个null if (fieldNames.Length != values.Length) { return null ; } Debug.Log( "table_name:" + table_name); command.CommandText = "insert into " + table_name + "(" ; for ( int i = 0; i < fieldNames.Length; i++) { command.CommandText += fieldNames[i]; if (i < fieldNames.Length - 1) { command.CommandText += "," ; } } command.CommandText += ")" + "values (" ; for ( int i = 0; i < values.Length; i++) { command.CommandText += values[i]; if (i < values.Length - 1) { command.CommandText += "," ; } } command.CommandText += ")" ; Debug.Log(command.CommandText); return command.ExecuteReader(); } // 【删除数据】 public SqliteDataReader DeleteData( string table_name, string [] conditions) { command.CommandText = "delete from " + table_name + " where " + conditions[0]; for ( int i = 1; i < conditions.Length; i++) { // or:表示或者 // and:表示并且 command.CommandText += " or " + conditions[i]; } return command.ExecuteReader(); } // 【修改数据】 public SqliteDataReader UpdateData( string table_name, string [] values, string [] conditions) { command.CommandText = "update " + table_name + " set " + values[0]; for ( int i = 1; i < values.Length; i++) { command.CommandText += "," + values[i]; } command.CommandText += " where " + conditions[0]; for ( int i = 1; i < conditions.Length; i++) { command.CommandText += " or " + conditions[i]; } return command.ExecuteReader(); } // 【查询数据】 public SqliteDataReader SelectData( string table_name, string [] fields) { command.CommandText = "select " + fields[0]; for ( int i = 1; i < fields.Length; i++) { command.CommandText += "," + fields[i]; } command.CommandText += " from " + table_name; return command.ExecuteReader(); } // 【查询整张表的数据】 public SqliteDataReader SelectFullTableData( string table_name) { command.CommandText = "select * from " + table_name; return command.ExecuteReader(); } // 【关闭数据库】 public void CloseDataBase() { connection.Close(); command.Cancel(); } } |
1 | 这里注意 连接数据库路径 //需带'Data Source='前缀 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using UnityEngine; using System.Collections; using Mono.Data.Sqlite; public class Test : MonoBehaviour { // Use this for initialization void Start() { // 数据库文件的具体路径,有的是.sqlite/.db string path = "Data Source=" +Application.streamingAssetsPath + "/" + "settingdata.db" ; Debug.Log( "path:" + path); SQLite sql = new SQLite(path); SqliteDataReader reader1 = sql.InsertData( "ddd" , new string [] { "name" , "score" }, new object [] { "'Sivan'" , 99 }); // 读取到的信息使用之后需要关闭 reader1.Close(); sql.CloseDataBase(); } // Update is called once per frame void Update() { } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix