找回密码
 立即注册
查看: 4320|回复: 86

u3d和sqlite连接实现插入数据了,相信删、改代码

[复制链接]
发表于 2013-3-5 08:40 | 显示全部楼层 |阅读模式
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Data;
  4. using Mono.Data.SqliteClient;
  5. using System;

  6. public class test : MonoBehaviour {
  7.          string txtname="111";
  8.          string txtip;
  9.         // Use this for initialization
  10.         void Start () {
  11.         
  12.         }
  13.         void OnGUI()
  14.         {
  15.                
  16.                 GUI.Label(new Rect(1,1,50,30),"13");
  17.                
  18.                 if(GUI.Button(new Rect(160,200,60,60),"asd"))
  19.                 {
  20.                         InsertInto();
  21.                         print("success");
  22.                 }
  23.                 GUI.TextArea(new Rect(20,100,60,60),"asdasd");
  24.                 OpenDB();
  25.                
  26.         }
  27.         public void Awake()
  28.         {
  29.                 print(Application.dataPath);
  30.                 }
  31.         public void OpenDB()
  32.         {
  33.                 string connectionString= "URI=file:play.db";
  34.                 IDbConnection dbcon = (IDbConnection) new SqliteConnection(connectionString);
  35.                 dbcon.Open();
  36.                 IDbCommand dbcmd = dbcon.CreateCommand();
  37.                 string strsql = "SELECT name,ip " + " FROM players";
  38.                 dbcmd.CommandText = strsql;
  39.                 IDataReader reader = dbcmd.ExecuteReader();
  40.                 int i=1;
  41.                 while(reader.Read()){
  42.                         string name = reader.GetString(0);
  43.                         string ip = reader.GetString(1);
  44.                         GUI.TextArea(new Rect(1,i*80,200,50),"name:"+name+"\nip:"+ip);
  45.                         i++;
  46.                 }
  47.                 reader.Close();
  48.                 reader = null;
  49.                 dbcmd.Dispose();
  50.                 dbcmd = null;
  51.                 dbcon.Close();
  52.                 dbcon = null;
  53.         }
  54.         public void InsertInto()
  55.         {
  56.                 string connectionString= "URI=file:play.db";
  57.                 IDbConnection dbcon = (IDbConnection) new SqliteConnection(connectionString);
  58.                 dbcon.Open();
  59.                 IDbCommand dbcmd = dbcon.CreateCommand();
  60.                 txtname = "test5";
  61.                 txtip = "asdasd.sdf";
  62.                 print(txtname+txtip);
  63.                 string strsql = "Insert into players (id,name,ip) VALUES (6,66,666)";
  64.                 dbcmd.CommandText = strsql;
  65.                 IDataReader reader = dbcmd.ExecuteReader();
  66.                
  67.                 reader.Close();
  68.                 reader = null;
  69.                 dbcmd.Dispose();
  70.                 dbcmd = null;
  71.                 dbcon.Close();
  72.                 dbcon = null;        
  73.         }
  74.         // Update is called once per frame
  75.         void Update () {
  76.         
  77.         }
  78. }
复制代码
sqlite
不支持
webplayer

 楼主| 发表于 2013-3-5 08:40 | 显示全部楼层
但是编译成ios后出错,据高手指点是xcode中数据库路径问题,未解决
发表于 2017-3-10 10:43 | 显示全部楼层
很不错
发表于 2017-3-10 11:12 | 显示全部楼层
真心顶
发表于 2017-3-10 10:26 | 显示全部楼层
说的非常好
发表于 2017-3-10 10:40 | 显示全部楼层
不错不错
发表于 2017-3-10 10:35 | 显示全部楼层
LZ真是人才
发表于 2017-3-17 21:49 | 显示全部楼层
很不错
发表于 2017-3-17 21:08 | 显示全部楼层
真心顶
发表于 2017-3-17 21:13 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-3 23:47 , Processed in 0.161149 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表