Asp tutorial. net C sqlite database tutorial method
A SQLite engine is required. There is a System. Data. SQLite, which can be used after being added to the project reference. Here is a simple reference:
SQLiteConnection mycon = new SQLiteConnection (@ "data source = dbPerson. db3 ");
Mycon. Open ();
SQLiteCommand cmd = mycon. CreateCommand ();
Cmd. CommandText = @ "select * from person ";
SQLiteDataAdapter da = new SQLiteDataAdapter (cmd );
DataSet ds = new DataSet ();
Da. Fill (ds );
DataGridView1.DataSource = ds. Tables [0];
Mycon. Close ();
You need to download the source code of sqlite
Http://www.sqlite.org/sqlite-3.6.6.2.tar.gz
# Tar xf sqlite-3.6.6.2.tar.gz
# Cd sqlite-3.6.6.2.tar.gz
#./Configure prefix =/usr
# Make
# Make install
Then .. You can start the first step. Access the sqlite database in c
The c code is as follows:
# Include <stdio. h>
# Include <stdlib. h>
# Include <sqlite3.h>
Int main (void)
{
Sqlite3 * db = NULL;
Char * zErrMsg = 0;
Int rc;
Rc = sqlite3_open ("zieckey. db", & db );
If (rc)
{
Fprintf (stderr, "Can't open sqlite: % sn", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
Else printf ("open sqlite successn ");
Sqlite3_close (db); // Close the database
Return 0;
}
Save this file as SQL. c
Link to the sqlite dynamic library
# Gcc SQL. c-lsqlite3-o SQL
You can also directly connect to the static library.
# Gcc SQL. c/usr/lib/libsqlite3.a-lpthread-o SQL
Run
#./SQL
Will display
Open sqlite success
Congratulations. Sqlite works properly.
Now we can work on it. Let's take a look at more detailed practices.
// Checkusername. cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
// Sqlite3_exec overload to avoid unnecessary parameters (pure C is not overloaded)
SQLITE_API int sqlite3_exec (sqlite3 * db, const char * SQL)
{
Char * zErrMsg = 0;
Return sqlite3_exec (db, SQL, NULL, NULL, & zErrMsg );
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
Printf ("Contenttype: text/htmlnn"); // according to the HTTP protocol, there must be a blank line.
Sqlite3 * db;
Int rc;
Rc = sqlite3_open ("D: greeninstalltinywebserverwwwrootrp. db3", & db );
If (rc! = SQLITE_ OK)
{
Printf ("Can't open database: % sn", sqlite3_errmsg (db ));
Sqlite3_close (db );
Return-1;
}
/* Rc = sqlite3_exec (db, "Insert into T_User (username, password) values ('admin', '123 ')");
If (rc! = SQLITE_ OK)
{
Printf ("Can't open database: % sn", sqlite3_errmsg (db ));
Sqlite3_close (db );
Return-1;
}*/
Sqlite3_stmt * pStmt;
// Creation process
Rc = sqlite3_prepare (db, "select * from T_User where password =? ",-1, & pStmt, 0 );
If (rc! = SQLITE_ OK ){
Printf ("execute SQL error: % sn", sqlite3_errmsg (db ));
Sqlite3_close (db );
Return-1;
}
// Bind parameters
If (sqlite3_bind_text (pStmt, 1, "123",-1, SQLITE_STATIC )! = SQLITE_ OK ){
Printf ("sqlite3_bind_int error: % sn", sqlite3_errmsg (db ));
Goto test_exit;
}
// Multiple execution processes
While (sqlite3_step (pStmt )! = SQLITE_DONE ){
Const unsigned char * name = sqlite3_column_text (pStmt, 1 );
Const unsigned char * password = sqlite3_column_text (pStmt, 2 );
Printf ("% s = % sn", name, password );
}
Test_exit:
If (sqlite3_finalize (pStmt )! = SQLITE_ OK ){
Printf ("testPrepareStmt-sqlite3_finalize ");
}
Sqlite3_close (db );
Printf ("OK ");
Return 0;
}