Unity3d 读取 Sqlite数据库

前端之家收集整理的这篇文章主要介绍了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上就可以。

猜你在找的Sqlite相关文章