Unity 3d sqlite Database connection
C
SharpScript 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.0
frameworkon build
settings
dbAccess.cs
using UnityEngine;
using System;
usingSystem.Collections;
usingSystem.Data;
using Mono.Data.sqliteClient;
public classdbAccess : 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(); // cleaneverything 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
returnreader; //returnthe reader
}
int CreateTable(string name,string[] col,string[] colType){ // Create a table,name,column array,column typearray
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( Exceptione){
Debug.Log(e);
return0;
}
return1;
}
int InsertIntoSingle(string tableName,string colName,string value ){ // singleinsert
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);
return0;
}
return1;
}
int InsertIntoSpecific(string tableName,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);
return0;
}
return1;
}
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);
return0;
}
return1;
}
public string[] SingleSelectWhere(string tableName,string itemToSelect,string wCol,string wPar,string wValue){ // Selects asingleItem
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); // Fillarraywith all matches
i++;
}
returnreadArray; //returnmatches
}
// Update is called once per frame
void Update () {
}
}
原文链接:https://www.f2er.com/sqlite/201935.htmldbAccess.cs
using UnityEngine;
using System;
usingSystem.Collections;
usingSystem.Data;
using Mono.Data.sqliteClient;
public classdbAccess : 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(); // cleaneverything 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
returnreader; //returnthe reader
}
int CreateTable(string name,string[] col,string[] colType){ // Create a table,name,column array,column typearray
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( Exceptione){
Debug.Log(e);
return0;
}
return1;
}
int InsertIntoSingle(string tableName,string colName,string value ){ // singleinsert
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);
return0;
}
return1;
}
int InsertIntoSpecific(string tableName,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);
return0;
}
return1;
}
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);
return0;
}
return1;
}
public string[] SingleSelectWhere(string tableName,string itemToSelect,string wCol,string wPar,string wValue){ // Selects asingleItem
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); // Fillarraywith all matches
i++;
}
returnreadArray; //returnmatches
}
// Update is called once per frame
void Update () {
}
}
The following few dlls have to be added inside
Assets/
Pluginsfolder