Unity 3d SQLite Database connection
来源:互联网 发布:java main函数 启动 编辑:程序博客网 时间:2024/05/21 05:44
Unity 3d SQLite Database connection
C Sharp Script file to use wtih unity 3D. This one works with unity pro <= version 2.6 only free versions you can test but cannot build. And for Unity 3D 3 and above they don't support building for webplayer, standalone builds only work and you have to set standard .net 2.0framework on build settings
dbAccess.cs
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using Mono.Data.SqliteClient;
public class dbAccess : MonoBehaviour {
private string connection;
private IDbConnection dbcon;
private IDbCommand dbcmd;
private IDataReader reader;
// Use this for initialization
void Start () {
}
public void OpenDB(string p)
{
connection = "URI=file:" + p; // we set the connection to our database
dbcon = new SqliteConnection(connection);
dbcon.Open();
}
public void CloseDB(){
reader.Close(); // clean everything up
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
IDataReader BasicQuery(string query){ // run a baic Sqlite query
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
return reader; // return the reader
}
int CreateTable(string name,string[] col, string[] colType){ // Create a table, name, columnarray, column type array
string query;
query = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
for(var i=1; i<col.length; i++){<="" p="">
query += ", " + col[i] + " " + colType[i];
}
query += ")";
try{
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertIntoSingle(string tableName, string colName , string value ){ // single insert
string query;
query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
try
{
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertIntoSpecific(string tableName, string[] col, string[] values){ // Specific insert with col and values
string query;
query = "INSERT INTO " + tableName + "(" + col[0];
for(int i=1; i<col.length; i++){<="" p="">
query += ", " + col[i];
}
query += ") VALUES (" + values[0];
for(int i=1; i<values.length; i++){<="" p="">
query += ", " + values[i];
}
query += ")";
try
{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertInto(string tableName , string[] values ){ // basic Insert with just values
string query;
query = "INSERT INTO " + tableName + " VALUES (" + values[0];
for(int i=1; i<values.length; i++){<="" p="">
query += ", " + values[i];
}
query += ")";
try
{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
public string[] SingleSelectWhere(string tableName , string itemToSelect,string wCol,string wPar, string wValue){ // Selects a single Item
string query;
query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
string[] readArray = new string[reader.RecordsAffected];
int i=0;
while(reader.Read()){
readArray[i]=reader.GetString(0); // Fill array with all matches
i++;
}
return readArray; // return matches
}
// Update is called once per frame
void Update () {
}
}
dbAccess.cs
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using Mono.Data.SqliteClient;
public class dbAccess : MonoBehaviour {
private string connection;
private IDbConnection dbcon;
private IDbCommand dbcmd;
private IDataReader reader;
// Use this for initialization
void Start () {
}
public void OpenDB(string p)
{
connection = "URI=file:" + p; // we set the connection to our database
dbcon = new SqliteConnection(connection);
dbcon.Open();
}
public void CloseDB(){
reader.Close(); // clean everything up
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
IDataReader BasicQuery(string query){ // run a baic Sqlite query
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
return reader; // return the reader
}
int CreateTable(string name,string[] col, string[] colType){ // Create a table, name, columnarray, column type array
string query;
query = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
for(var i=1; i<col.length; i++){<="" p="">
query += ", " + col[i] + " " + colType[i];
}
query += ")";
try{
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertIntoSingle(string tableName, string colName , string value ){ // single insert
string query;
query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
try
{
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertIntoSpecific(string tableName, string[] col, string[] values){ // Specific insert with col and values
string query;
query = "INSERT INTO " + tableName + "(" + col[0];
for(int i=1; i<col.length; i++){<="" p="">
query += ", " + col[i];
}
query += ") VALUES (" + values[0];
for(int i=1; i<values.length; i++){<="" p="">
query += ", " + values[i];
}
query += ")";
try
{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
int InsertInto(string tableName , string[] values ){ // basic Insert with just values
string query;
query = "INSERT INTO " + tableName + " VALUES (" + values[0];
for(int i=1; i<values.length; i++){<="" p="">
query += ", " + values[i];
}
query += ")";
try
{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}
public string[] SingleSelectWhere(string tableName , string itemToSelect,string wCol,string wPar, string wValue){ // Selects a single Item
string query;
query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
string[] readArray = new string[reader.RecordsAffected];
int i=0;
while(reader.Read()){
readArray[i]=reader.GetString(0); // Fill array with all matches
i++;
}
return readArray; // return matches
}
// Update is called once per frame
void Update () {
}
}
The following few dlls have to be added inside Assets/Plugins folder
Mono.Data.Sqlite.dll
Mono.Data.SqliteClient.dll
MySql.Data.dll
sqlite3.dll
System.Data.dll
- Unity 3d SQLite Database connection
- SQLite C Interface - Opening A New Database Connection
- SQLite C Interface - Opening A New Database Connection
- Qt13 Creating connection open and close function with sqlite database
- DataBase Connection
- [SQLite]---database
- Unity 3D
- unity 3d 注意事项
- Unity 3d 摄像头
- Unity 3D 使用
- Unity 3D--PlayerPrefs
- Unity 3d
- Unity 3D俄罗斯方块
- unity 3d 多开
- UNITY 3D笔记
- Unity 3D接触
- unity 3d
- Unity 3D简介
- 【bash】通过实践学习bash之检测局域网主机状态
- OPENLDAP安装、配置
- MAVEN,CXF,设计模式,LINUX SHELL,EJB,JMS
- DWR学习笔记(三)
- OCCI开发环境搭建
- Unity 3d SQLite Database connection
- 新手上路第一天————菜鸟的心得之单链表(循环链表)
- 【web前端】JavaScript实现图片幻灯片滚动播放动画效果
- PHY+MAC
- jQuery操作radio、checkbox、select总结
- JS 中面向对象的5种写法
- Ext.form.FormPanel表单提交
- sharepoint 2007 直接使用文件的链接访问站点文件时无法访问,sharepoint报错
- C#创建xml