asp教程.net C sqlite資料庫教程的方法
需要一個SQLite的引擎啊,有個System.Data.SQLite,添加到項目引用之後就可以用了,給你個簡單的參考:
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();
你需要下載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
然後。。就可以開始第一步嘗試了。在c中訪問sqlite資料庫
c代碼如下:
#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); //關閉資料庫
return 0;
}
將此檔案另存新檔sql.c
可以連結sqlite動態庫
#gcc sql.c -lsqlite3 -o sql
也可以直接連接靜態庫
#gcc sql.c /usr/lib/libsqlite3.a -lpthread -o sql
執行
#./sql
會顯示
open sqlite success
恭喜你。sqlite可以正常工作了
下面可以工作了,我們看更詳細的做法
// checkusername.cpp : 定義控制台應用程式的進入點。
//
#include "stdafx.h"
//sqlite3_exec的重載,避免穿那麼多用不到的參數(純C沒有重載)
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"); //根據HTTP協議,這裡一定要有個空行。
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;
//建立過程
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;
}
//綁定參數
if(sqlite3_bind_text(pStmt, 1, "123",-1,SQLITE_STATIC) != SQLITE_OK){
printf( "sqlite3_bind_int error: %sn", sqlite3_errmsg(db));
goto test_exit;
}
// 多次執行過程
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;
}