码迷,mamicode.com
首页 > 数据库 > 详细

MySQL

时间:2016-06-26 19:52:08      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

http://www.runoob.com/sqlite/sqlite-expressions.html

 

封装好的sql

using UnityEngine;

using System;
using System.Collections;
using Mono.Data.Sqlite;


public class DbAccess

{


private SqliteConnection dbConnection;

private SqliteCommand dbCommand;

private SqliteDataReader reader;


public DbAccess (string connectionString)

{

OpenDB (connectionString);

}
public DbAccess ()
{

}

public void OpenDB (string connectionString)

{
try
{
dbConnection = new SqliteConnection (connectionString);

dbConnection.Open ();

Debug.Log ("Connected to db");
}
catch(Exception e)
{
string temp1 = e.ToString();
Debug.Log(temp1);
}

}

 

public void CloseSqlConnection ()

{

if (dbCommand != null) {

dbCommand.Dispose ();

}

dbCommand = null;

if (reader != null) {

reader.Dispose ();

}

reader = null;

if (dbConnection != null) {

dbConnection.Close ();

}

dbConnection = null;

Debug.Log ("Disconnected from db.");

}


public SqliteDataReader ExecuteQuery (string sqlQuery)

{

dbCommand = dbConnection.CreateCommand ();

dbCommand.CommandText = sqlQuery;

 

reader = dbCommand.ExecuteReader ();

 

 

return reader;

}


public SqliteDataReader ReadFullTable (string tableName)

{

string query = "SELECT * FROM " + tableName;

return ExecuteQuery (query);

}

 

public SqliteDataReader InsertInto (string tableName, string[] values)

{

string query = "INSERT INTO " + tableName + " VALUES (" + values[0];

for (int i = 1; i < values.Length; ++i) {

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

}

query += ")";

return ExecuteQuery (query);

}


public SqliteDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue)
{


string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0];

for (int i = 1; i < colsvalues.Length; ++i) {

query += ", " +cols[i]+" ="+ colsvalues[i];
}

query += " WHERE "+selectkey+" = "+selectvalue+" ";

return ExecuteQuery (query);
}


public SqliteDataReader Delete(string tableName,string []cols,string []colsvalues)
{
string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0];

for (int i = 1; i < colsvalues.Length; ++i) {

query += " or " +cols[i]+" = "+ colsvalues[i];
}
return ExecuteQuery (query);
}


public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values)

{

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

throw new SqliteException ("columns.Length != values.Length");

}

string query = "INSERT INTO " + tableName + "(" + cols[0];

for (int i = 1; i < cols.Length; ++i) {

query += ", " + cols[i];

}

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

for (int i = 1; i < values.Length; ++i) {

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

}

query += ")";

return ExecuteQuery (query);

}

 

public SqliteDataReader DeleteContents (string tableName)

{

string query = "DELETE FROM " + tableName;

return ExecuteQuery (query);

}


public SqliteDataReader CreateTable (string name, string[] col, string[] colType)

{

if (col.Length != colType.Length) {

throw new SqliteException ("columns.Length != colType.Length");

}

string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

for (int i = 1; i < col.Length; ++i) {

query += ", " + col[i] + " " + colType[i];

}

query += ")";


return ExecuteQuery (query);

}




public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)

{

if (col.Length != operation.Length || operation.Length != values.Length) {

throw new SqliteException ("col.Length != operation.Length != values.Length");

}

string query = "SELECT " + items[0];

for (int i = 1; i < items.Length; ++i) {

query += ", " + items[i];

}

query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "‘" + values[0] + "‘ ";

for (int i = 1; i < col.Length; ++i) {

query += " AND " + col[i] + operation[i] + "‘" + values[0] + "‘ ";

}


return ExecuteQuery (query);

 

}

 

 

 

}

 

 

 

一般调用

 

using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.IO;

 

public class ZYSQLTest : MonoBehaviour {

 

//创建表
void CreateMyCul()
{
string appDBPath = Application.persistentDataPath + "/zhongyingying.db";

if (!File.Exists(appDBPath))
{

 

DbAccess db = new DbAccess("URI=file:" + appDBPath);
db.CreateTable("zy", new string[] { "name", "age", "rank" }, new string[] { "text", "text", "text" });

Debug.Log("CreateSuccessful!创建表成功!");
db.CloseSqlConnection();

 

}

else
{
Debug.Log("File Exists!已经存在了!");
}

 

}
//插入数据
void InsertMyData()
{
string appDBPath = Application.persistentDataPath + "/zhongyingying.db";

DbAccess db = new DbAccess("URI=file:" + appDBPath);
Debug.Log(db);
db.InsertInto("zy", new string[] { "‘missZhong‘", "‘24‘", "‘1‘" });
db.InsertInto("zy", new string[] { "‘MrZhang‘", "‘22‘", "‘5‘" });

 

db.CloseSqlConnection();
Debug.Log("InsertInfoSuccessful!!!插入值成功!");

 

}
//删除数据
void DeleteMySelectedData()
{
string appDBPath = Application.persistentDataPath + "/zhongyingying.db";
DbAccess db = new DbAccess("URI=file:" + appDBPath);
db.Delete("zy", new string[] { "‘age‘" }, new string[] { "‘24‘" });
db.CloseSqlConnection();
Debug.Log("deleteSuccessful!删除成功!");
}
//查询数据
void SelectMyData()
{
string appDBPath = Application.persistentDataPath + "/zhongyingying.db";
DbAccess db = new DbAccess("URI=file:" + appDBPath);
SqliteDataReader sdr = db.SelectWhere("zy", new string[] { "age", "rank" }, new string[] { "age" }, new string[] { "=" }, new string[] { "24" });
db.CloseSqlConnection();
Debug.Log("SelectSuccessful!查询成功!");
Debug.Log("查询到的数据是______"+db);

 


}
//数据展示
void OnGUI()
{
if (GUI.Button(new Rect(350, 50, 100, 100), "创建表"))
{
CreateMyCul();
}
if (GUI.Button(new Rect(350, 200, 100, 100), "插入数据"))
{
InsertMyData();
}
if (GUI.Button(new Rect(350, 350, 100, 100), "删除数据"))
{
DeleteMySelectedData();
}
if (GUI.Button(new Rect(350, 500, 100, 100), "查询数据"))
{
SelectMyData();
}
}
}

 

MySQL

标签:

原文地址:http://www.cnblogs.com/ZeroMurder/p/5618407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!