Installing SQLite and common operations

Source: Internet
Author: User

SQLite isData Storage Basics, let's start with SQLite today.What is SQLite Sqliteis a very lightweight relational database system that supports mostSQL92Standard. SQLiteinstallation settings are not required before use, and processes are not required to start, stop, or configure, while most otherSQLThe database engine is a separate server process that is used by the program to communicate with some kind of internal process(typical of this isTCP/IP)To complete the job of sending the request to the server and receiving the query results,SQLitedo not use this way of working. UseSQLite, the program accessing the database reads and writes directly from the database file on the disk without intermediate server processes. UseSQLitegenerally only need to bring aDLL, you can use all of its features. SqliteThe main application scenarios are databases for mobile phone applications and small desktop software. install using SQLite Sqlitethe official forhttp://www.sqlite.org/download.html, which provides a variety of versions of theSQLite, I chose to download the nameSqlite-shell-win32-x86-3080500.zipthe version. After downloading it directly to the disk, you can see that after the decompression onlySqlite3.exethis file. Next you need toSQLiteadded toPathEnvironment variables (environment variables are added to make it easier to useSQLite), right-click My Computer-Properties-Advanced system Settings-environment variables, found in system variablesPath, add the extracted folder directory to the back (note the folder directory, such as my local directoryE:\\tools\\sqlite). Opencmd, enterSqlite3, the following message pops up, indicating success. SQLite Common operations 1.Create a new database file>command line to enter to createDBfolder location for files>To create a database file by using a command: Sqlite3that you want to create.DBfile name>to view attached database files by using a command:. Datas2.open an established database file>the command line goes to theDBfolder location for files>use the command line to open an establishedDBFile:Sqlite3file name (note:If the file name does not exist, a newDBfiles)3.View Help Commands>command line input directlySqlite3, go in toSqlite3Command Line Interface>input. HelpView common Commandsusing the sqlite management tool ShellAlthough the script provides a very powerful function, but it is not easy to use, fortunately,SQLiteThere are a lot of open source and excellentDBMS! Here I will use a model calledSqlitespySoftware, the official website address isHttp://www.yunqa.de/delphi/doku.php/products/sqlitespy/index, this software is green free installation version, decompression directly run on it. can see thatSqlitespythe interface layout andSQL Serververy close, operation is very convenient, here is not continue to detailed introduction. (One thing to know is that using this software alone can create and useSQLitedatabase, which is not required with the above mentionedShellTool Association)C # uses System.Data.SQLite.dll to access the database Sqliteprovides aC #called byDLL, forHttp://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki, note according to. NET FRAMEWORKversion to download the corresponding component. In the project, just introduceSystem.Data.SQLite.dllThis component, you can implement the database operation. BecauseSQLite.dllrealized theADOinterface, so familiarize yourself with theADOpeople to get startedSQLite.dllis also very fast. DEMOthe structure of a database table is:CREATE TABLE Hero (hero_id INT not null PRIMARY key,hero_name NVARCHAR (TEN) not null); the comparison needs to be noted that the database connection stringSQLiteThe connection string used is simpler, as long as the reference path to the database file is written. DEMOis a console application, adding and deleting the instance code of the check and change is as follows:Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.data.common;using system.data.sqlite;namespace consoleapp{class program{static readonly string DB_PATH = " Data source=e:/data/sqlite/arena.db "; static void Select () {using (sqliteconnection con = new Sqliteconnection (Db_path)) {con. Open (); string sqlstr = @ "select *from Hero"; using (sqlitecommand cmd = new Sqlitecommand (Sqlstr,con)) {using ( Sqlitedatareader dr = cmd. ExecuteReader ()) {while (Dr. Read ()) {Console.WriteLine (dr["hero_id"). ToString () + dr["Hero_name"]);}}}} static void Insert () {using (sqliteconnection con = new Sqliteconnection (Db_path)) {con. Open (); string sqlstr = @ "INSERT into herovalues (1, 'Shaman'), using (sqlitecommand cmd = new Sqlitecommand (Sqlstr,con)) {cmd. ExecuteNonQuery ();}}} static void Update () {using (sqliteconnection con = new Sqliteconnection (Db_path)) {con. Open (); string sqlstr = @ "UPDATE heroset hero_name ="Thieves'WHERE hero_id = 1 "; using (sqlitecommand cmd = new Sqlitecommand (Sqlstr, con)) {cmd. ExecuteNonQuery ();}}} static void Delete () {using (sqliteconnection con = new Sqliteconnection (Db_path)) {con. Open (); string sqlstr = @ "DELETE from hero"; using (sqlitecommand cmd = new Sqlitecommand (Sqlstr, con)) {cmd. ExecuteNonQuery ();}}} static void Main (string[] args) {Insert (); Select (); Update (); Select ();D elete ();}}} Original from:Codeceo

Installing SQLite and common operations

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.