Unity&sqlite Database

Source: Internet
Author: User
Tags sqlite sqlite database

SQLite is a cross-platform relational small database, very convenient, suitable for embedded devices, for the SQLite database, This database is formed in the form of a file (such as data.db); a database is made up of tables, where multiple tables can be stored in a database, and there is often a relationship between multiple tables.

For a table operation: Add and remove changes, statements and SQL Server statements, and in the table, there is a primary key (cannot be empty, can not be repeated, the addition of self-enhancement), foreign keys (and other tables associated), unique keys (unique can be empty, cannot be duplicated).

In the console, use SQLite's statement as follows:

Sqlite3 data.db; Open the database, no words to create a

. Table; View a few tables in the database

creat table USER (UID integer, name text, score integer); There are three fields in the CREATE TABLE table (can not write type, no type can be any type)

Insert into user values (1, ' Guo Jing ', 89)//Inserts a new piece of data into the user table

SELECT * from USER; View all content in the current table

drop table USER; Delete User table

CREATE table if not exists USER (UID Integer primary key autoincrement, name text, score integer); Integer UID self-increment

Insert into USER (name, score) VALUES (' Yang over ', 99); UID from Add data

Update USER set name= ' yellow old evil ' where score=89; Modify the artificial yellow and old evil of score to 89

Delete from USER; Delete all data

Delete from USER where uid = 1; Delete data with UID 1 in table

Select Uid,name,score from USER; Find content in a table

Select name from USER;

Select COUNT (*) from USER; Find a few rows of data in the user table

Select Sun (score) from USER; Look up all the score in the table and

Select AVG (score) from USER; Find the average number of score

SELECT * from USER where score>90 and score<95; Finding data in tables with score greater than 90 and less than 95

SELECT * from USER limit 2; Find the first two articles now

SELECT * Form USER ORDER BY score//Sort by score size order

Select *from USER order by score DECs; Sort in reverse order according to the size of score

Select User.Name, user.score,kungfu.name from User,kungfu where User.uid=kungfu.uid; Federated queries

. exit; Exit

SQLite is used in unity, you need to create a plugins folder in Project->assest, and then add the database in

The code is as follows:

Using system.collections;using system.collections.generic;using unityengine;using mono.data.sqlite;using System; public class Sqlitetext:monobehaviour {sqliteconnection con;//database connection class void Start () {//Connect to database, if not, create a database con = new Sqlitec Onnection ("Data Source =" + application.database + "/data/data.db"); con. Open ();//Opening database//Creating table String sqlstr= "CREATE table if not exist USER (UID Integer primary key autoincrement, name text, score I Nteger) "; Sqlitecommand command =new Sqlitecommand (Sqlstr,con);//Execution of commands (no queries, for additions and deletions) command. ExecuteNonQuery ()///Close command (since SQLite is single-threaded, the command should be closed after each execution of the command). Dispose ();//Insert Data SQLSTR = "INSERT into USER (Name,score) VALUES (' King Sledgehammer '," "); command. ExecuteNonQuery (sqlstr,con); command. Dispose (); sqlstr = "SELECT count (*) from USER";//Query single result and convert to int counts = Convert.ToInt32 (command). ExecuteScalar ()); command. Dispose ();//Query multiple results sqlitedatareader read result class Sqlstr = "select * from USER"; command = new Sqlitecommand (sqlstr, con); Sqlitedatareader reader = command. ExecuteReader ();//sQlitedatareader the logic of fetching data in a table//first, by default there is a pointer to the table header, there is a way to move the pointer down one line//and then get the corresponding value by the number of columns, then move the pointer down//read () read one line while (reader). Read ()) {//remove uidint uid = reader. GetInt32 (0);//Remove namestring name = Reader. GetString (1);//Get name//name = reader["name" in dictionary mode). ToString ();//Remove scorestring score = reader. GetInt32 (2);} Command. Dispose (); reader. Close ();} void Destroy () {con. Close ();//Shut down the database}}

  

Unity&sqlite Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.