Unity3d 读取 Sqlite数据库

首先需要一个sqlite的DLL文件,在Unity安装目录可以找到。


http://download.csdn.net/detail/cp790621656/8017075



代码如下:

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

public class Helper : MonoBehaviour {

	private SqliteConnection mSqliteConnection;
	private SqliteCommand mSqliteCommand;
	private SqliteDataReader mSqliteDataReader;

	// Use this for initialization
	void Start () {

		//打开数据库
		try
		{
			string dbpath=Application.dataPath+"/game.db";
			mSqliteConnection = new SqliteConnection("URI=file:"+dbpath);
			mSqliteConnection.Open();
		}
		catch(Exception e)
		{
			Debug.Log(e.ToString());
		}

		//查询一个数据
		string sqlquerystr = "select * from type_skill_attribute where id < 30";
		mSqliteCommand = mSqliteConnection.CreateCommand ();
		mSqliteCommand.CommandText = sqlquerystr;
		mSqliteDataReader = mSqliteCommand.ExecuteReader ();

		while(mSqliteDataReader.Read())
		{
			int customid=mSqliteDataReader.GetOrdinal("skill_id"); //获取列ID,从0开始
			Debug.Log("customid = "+customid);
			int skill_id=mSqliteDataReader.GetInt32(customid);

			customid=mSqliteDataReader.GetOrdinal("type");
			Debug.Log("customid = "+customid);
			string type=mSqliteDataReader.GetString(customid);

			Debug.Log("skill_id = "+skill_id+" type = "+type);
		}

		mSqliteDataReader.Close ();
		if(mSqliteCommand!=null)
		{
			mSqliteCommand.Dispose ();
		}
		if(mSqliteConnection!=null)
		{
			mSqliteConnection.Close ();
            mSqliteConnection = null;
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

挂载到Camera上就可以。

你可能感兴趣的:(Unity,sqlite,数据库,unity)