sqlite3 是一种小型数据库,主要是记录一些命令的用法。
终端命令
sqlite3 文件名
在当前目录下,打开或新建数据库,并进入sqlite3模式下。(前提是把sqlite3程序路径加到环境变量中,不然就需要加上sqlite3的路径)
select * from 表格名称 where 表格列名1=X and 表格列名2=X;
在表中查找 符合条件的所有行。
c++代码中命令
在代码中打开或新建数据库。
int rc = sqlite3_open(dataFilePath.c_str(), &db);
if (rc != SQLITE_OK){
sqlite3_close(db); db = NULL; log("sqlite file %s open fail!", dataFilePath.c_str());
return;
}
获取数据库对应数据
sqlite3_stmt* statement = NULL;
char sql_select[200] = "select * from npc where id = ";
sprintf(sql_select,"select * from npc where id = %d",_id);
if (sqlite3_prepare_v2(db,sql_select,-1,&statement,NULL) != SQLITE_OK){
return IsSuccess;
}
int ncolumn = sqlite3_column_count(statement);
while (sqlite3_step(statement) == SQLITE_ROW)
{
for (int i = 0; i < ncolumn; i++)
{
int vtype = sqlite3_column_type(statement, i);
const char *column_name = sqlite3_column_name(statement, i);
switch (vtype)
{
case SQLITE_NULL:
log("%s: null\n", column_name);
break;
case SQLITE_INTEGER:
log("%s: %d\n", column_name, sqlite3_column_int(statement, i));
break;
case SQLITE_FLOAT:
log("%s: %f\n", column_name, sqlite3_column_double(statement, i));
break;
case SQLITE_BLOB: // arguably fall through...
log("%s: BLOB\n", column_name);
break;
case SQLITE_TEXT:
log("%s: %s\n", column_name, sqlite3_column_text(statement, i));
break;
default:
log("%s: ERROR [%s]\n", column_name, sqlite3_errmsg(db));
break;
}
}
}