Afficher le didacticiel SQLite Unity3d (Android, Windows Phone, Windows, IOS, WINRT)
https://shorturl.edafait.com/?SQLiteUnity3d
https://shorturl.edafait.com/?S'abonner
en utilisant Mono.Data.Sqlite ;
utiliser le système ;
en utilisant System.Data ;
en utilisant System.IO ;
en utilisant UnityEngine.UI ;
connexion de chaîne privée, sqlQuery ;
IDbConnection dbconn ;
IDbCommand dbcmd ;
lecteur IDataReader privé ;
string DatabaseName = "Votrenom de base de données.s3db" ;
chaîne chemin de fichier = Application.dataPath + "/Plugins/" + DatabaseName ;
chaîne chemin de fichier = Application.persistentDataPath + "/" + DatabaseName ;
// If not found on android will create Tables and database
Debug.LogWarning("File "" + filepath + "" does not exist. Attempting to create from "" +
Application.dataPath + "!/assets/Employers");
// #UNITY_ANDROID
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/Employers.s3db");
while (!loadDB.isDone) { }
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDB.bytes);
// #UNITY_IOS
var loadDb = Application.dataPath + "/Raw/" + DatabaseName;
// this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
// #UNITY_WP8
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName;
// this is the path to your StreamingAssets in iOS
//then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
// #UNITY_WINRT
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName;
// this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
conn = "URI=file:" + filepath;
Debug.Log("Stablishing connection to: " + conn);
dbconn = new SqliteConnection(conn);
dbconn.Open();
Cet exemple :
Nom de la table (personnel) L'ID est la CLÉ PRIMAIRE, le nom, l'adresse
string query;
query = "CREATE TABLE Staff (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name varchar(100), Address varchar(200))";
try
{
dbcmd = dbconn.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);
}
string Name_readers, Address_readers;
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT Name, Address " + "FROM Staff";// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
Name_readers = reader.GetString(0);
Address_readers = reader.GetString(1);
Debug.Log(" name =" + Name_readers + "Address=" + Address_readers);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
dbcmd = dbconn.CreateCommand();
sqlQuery = string.Format("insert into Staff (name, Address) values ("{0}","{1}")", name, Address);// table name
dbcmd.CommandText = sqlQuery;
dbcmd.ExecuteScalar();
dbconn.Close();
}
Debug.Log("Insert Done ");
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
dbcmd = dbconn.CreateCommand();
sqlQuery = string.Format("UPDATE Staff set name = @name ,address = @address where ID = @id ");
SqliteParameter P_update_name = new SqliteParameter("@name", update_name);
SqliteParameter P_update_address = new SqliteParameter("@address", update_address);
SqliteParameter P_update_id = new SqliteParameter("@id", update_id);
dbcmd.Parameters.Add(P_update_name);
dbcmd.Parameters.Add(P_update_address);
dbcmd.Parameters.Add(P_update_id);
dbcmd.CommandText = sqlQuery;
dbcmd.ExecuteScalar();
dbconn.Close();
Search_function(t_id.text);
}
using (dbconn = new SqliteConnection(conn))
{
string Name_readers_Search, Address_readers_Search;
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT name,address " + "FROM Staff where id =" + Search_by_id;// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
Name_readers_Search = reader.GetString(0);
Address_readers_Search = reader.GetString(1);
data_staff.text += Name_readers_Search + " - " + Address_readers_Search + "n";
Debug.Log(" name =" + Name_readers_Search + "Address=" + Address_readers_Search);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
Https://Webxr.edafait.com
https://shorturl.edafait.com/?fZVHLor
https://shorturl.edafait.com/?zuS4kvW