de (csharp):
- using UnityEngine;
- using System;
- using Mono.Data.Sqlite;
- public class DbAccess
- {
- private SqliteConnection dbConnection;
- private SqliteCommand dbCommand;
- private SqliteDataReader reader;
- /// <summary>
- /// string to connect. The simpliest one looks like "URI=file:filename.db"
- /// </summary>
- /// <param name="connectionString">
- /// A <see cref="System.String"/>
- /// </param>
- public DbAccess (string connectionString)
- {
- OpenDB (connectionString);
- }
- /// <summary>
- /// The same as <see cref="DbAccess#Dbaccess" / >
- /// </summary>
- /// <param name="connectionString">
- /// A <see cref="System.String"/>
- /// </param>
- public void OpenDB (string connectionString)
- {
- dbConnection = new SqliteConnection (connectionString);
- }
- /// <summary>
- /// Closes connection to db
- /// </summary>
- public void CloseSqlConnection ()
- {
- if (dbCommand != null) {
- }
- dbCommand = null;
- if (reader != null) {
- }
- reader = null;
- if (dbConnection != null) {
- }
- dbConnection = null;
- }
- /// <summary>
- /// Executes query given by sqlQuery
- /// </summary>
- /// <param name="sqlQuery">
- /// query
- /// A <see cref="System.String"/>
- /// </param>
- /// <returns>
- /// null, if any error
- /// result of query, otherwise
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader ExecuteQuery (string sqlQuery)
- {
- dbCommand = dbConnection.CreateCommand ();
- dbCommand.CommandText = sqlQuery;
- reader = dbCommand.ExecuteReader ();
- return reader;
- }
- /// <summary>
- /// Selects everything from table
- /// </summary>
- /// <param name="tableName">
- /// name of table
- /// A <see cref="System.String"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader ReadFullTable (string tableName)
- {
- string query = "SELECT * FROM " + tableName;
- return ExecuteQuery (query);
- }
- /// <summary>
- /// Inserts data into table
- /// </summary>
- /// <param name="tableName">
- /// name of table to insert data
- /// A <see cref="System.String"/>
- /// </param>
- /// <param name="values">
- /// array of data in string representation
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader InsertInto (string tableName, string[] values)
- {
- string query = "INSERT INTO " + tableName + " VALUES (" + values[0];
- query += ", " + values[i];
- }
- query += ")";
- return ExecuteQuery (query);
- }
- /// <summary>
- /// Inserts data into specific columns of table
- /// </summary>
- /// <param name="tableName">
- /// name of table
- /// A <see cref="System.String"/>
- /// </param>
- /// <param name="cols">
- /// name of columns
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <param name="values">
- /// values
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values)
- {
- throw new SqliteException ("columns.Length != values.Length");
- }
- string query = "INSERT INTO " + tableName + "(" + cols[0];
- query += ", " + cols[i];
- }
- query += ") VALUES (" + values[0];
- query += ", " + values[i];
- }
- query += ")";
- return ExecuteQuery (query);
- }
- /// <summary>
- /// deletes any data from table
- /// </summary>
- /// <param name="tableName">
- /// table name
- /// A <see cref="System.String"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader DeleteContents (string tableName)
- {
- string query = "DELETE FROM " + tableName;
- return ExecuteQuery (query);
- }
- /// <summary>
- /// Creates table with specified columns
- /// </summary>
- /// <param name="name">
- /// table name to be created
- /// A <see cref="System.String"/>
- /// </param>
- /// <param name="col">
- /// array, containing names of columns
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <param name="colType">
- /// array, containing types of columns
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- {
- throw new SqliteException ("columns.Length != colType.Length");
- }
- query += ", " + col[i] + " " + colType[i];
- }
- query += ")";
- return ExecuteQuery (query);
- }
- /// <summary>
- /// Selects from table with specified parameters.
- /// Ex: SelectWhere("puppies", new string[] = {"breed"}, new string[] = {"earType"}, new string[] = {"="}, new string[] = {"floppy"});
- /// the same as command: SELECT breed FROM puppies WHERE earType = floppy
- /// </summary>
- /// <param name="tableName">
- /// name of table to select
- /// A <see cref="System.String"/>
- /// </param>
- /// <param name="items">
- /// item names
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <param name="col">
- /// array, containing columns of parameters
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <param name="operation">
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <param name="values">
- /// A <see cref="System.String[]"/>
- /// </param>
- /// <returns>
- /// result of query
- /// A <see cref="SqliteDataReader"/>
- /// </returns>
- public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)
- {
- throw new SqliteException ("col.Length != operation.Length != values.Length");
- }
- string query = "SELECT " + items[0];
- query += ", " + items[i];
- }
- query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
- query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";
- }
- return ExecuteQuery (query);
- }
- }
de (JavaScript):
- #pragma strict
- /* Script for testing out SQLite in Javascript
- 2011 - Alan Chatham
- Released into the public domain
- This script is a GUI script - attach it to your main camera.
- It creates/opens a SQLite database, and with the GUI you can read and write to it.
- */
- // This is the file path of the database file we want to use
- // Right now, it'll load TestDB.sqdb in the project's root folder.
- // If one doesn't exist, it will be automatically created.
- public var DatabaseName : String = "TestDB.sqdb";
- // This is the name of the table we want to use
- public var TableName : String = "TestTable";
- var db : dbAccess;
- function Start() {
- // Give ourselves a dbAccess object to work with, and open it
- db = new dbAccess();
- db.OpenDB(DatabaseName);
- // Let's make sure we've got a table to work with as well!
- var tableName = TableName;
- //var columnNames = new Array("firstName","lastName");
- var columnNames = new Array("firstName","lastName", "age"); // o negocio e mais embaixo
- //var columnValues = new Array("text","text");
- var columnValues = new Array("text","text", "text");
- try {
- db.CreateTable(tableName,columnNames,columnValues);
- }
- catch(e) {// Do nothing - our table was already created
- //- we don't care about the error, we just don't want to see it
- }
- }
- // These variables just hold info to display in our GUI
- var firstName : String = "First Name";
- var lastName : String = "Last Name";
- var age : String = "Age";
- var DatabaseEntryStringWidth = 100;
- var scrollPosition : Vector2;
- var databaseData : ArrayList = new ArrayList();
- // This GUI provides us with a way to enter data into our database
- // as well as a way to view it
- function OnGUI() {
- GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"");
- GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100));
- // This first block allows us to enter new entries into our table
- GUILayout.BeginHorizontal();
- firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth));
- lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth));
- age = GUILayout.TextField(age, GUILayout.Width (DatabaseEntryStringWidth));
- GUILayout.EndHorizontal();
- if (GUILayout.Button("Add to database")) {
- // Insert the data
- //InsertRow(firstName,lastName);
- InsertRow(firstName,lastName,age);
- // And update the readout of the database
- databaseData = ReadFullTable();
- }
- // This second block gives us a button that will display/refresh the contents of our database
- GUILayout.BeginHorizontal();
- if (GUILayout.Button ("Read Database"))
- databaseData = ReadFullTable();
- if (GUILayout.Button("Clear"))
- databaseData.Clear();
- GUILayout.EndHorizontal();
- GUILayout.Label("Database Contents");
- scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
- for (var line : ArrayList in databaseData) {
- GUILayout.BeginHorizontal();
- for (var s in line) {
- GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth));
- }
- GUILayout.EndHorizontal();
- }
- GUILayout.EndScrollView();
- if (GUILayout.Button("Delete All Data")) {
- DeleteTableContents();
- databaseData = ReadFullTable();
- }
- GUILayout.EndArea();
- }
- // Wrapper function for inserting our specific entries into our specific database and table for this file
- //function InsertRow(firstName:String, lastName:String) {
- function InsertRow(firstName:String, lastName:String, age:String) { //
- //var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"));
- var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"),("'"+age+"'"));
- db.InsertInto(TableName, values);
- }
- // Wrapper function, so we only mess with our table.
- function ReadFullTable() {
- return db.ReadFullTable(TableName);
- }
- // Another wrapper function...
- function DeleteTableContents() {
- db.DeleteTableContents(TableName);
- }
댓글 없음:
댓글 쓰기