sqlite3的sql语句用法总结与SQLite生成.db-journal文件问题

sqlite3的sql语句用法总结与SQLite生成.db-journal文件问题,第1张

概述这几天遇到了一个问题就是在insert以及update数据的时候,总是生成一个.db-journal文件,重新启动程序后,该文件消失,前面添加修改的数据没有成功保存。关于如何解决这个问题,网上也没有人给出详细答案,但经过几天的分析,试验,最后成功了。不知道对所有出现的这个问题都能应付的过来,解决的方法很简单,如下: 程序中使用了 sqlite3_stmt *stmt; 然后通过rc = sqlit

这几天遇到了一个问题就是在insert以及update数据的时候,总是生成一个.db-journal文件,重新启动程序后,该文件消失,前面添加修改的数据没有成功保存。关于如何解决这个问题,网上也没有人给出详细答案,但经过几天的分析,试验,最后成功了。不知道对所有出现的这个问题都能应付的过来,解决的方法很简单,如下:

程序中使用了

sqlite3_stmt*stmt;

然后通过rc =sqlite3_prepare(db,sql,strlen(sql),&stmt,&tail);将SQL语句解析到stmt中,然后对数据库 *** 作完成之后,没有释放stmt,当我调用sqlite3_finalize(stmt);将stmt释放之后,上面的问题不在出现了。

对此sqlite中还有一对匹配的用法。如下:

sqlite3_get_table( db,&azResult,&nrow,&ncolumn,&zErrMsg );

sqlite3_free_table( azResult );

下面是一些SQL语句的用法。

sprintf(sql,"INSERT INTO \"Phonebook\" VALUES((select min(ID)-1 from Phonebook where ID>1 and ID-1 not in (select ID from Phonebook)),\'%s\',\'%s\');",p->List[1],p->List[2]);

char sql1[100];
sprintf(sql1,"select * from Phonebook where Username like \'%s%%\'order by Username;",buffer);

上面是关于like匹配的用法,Username like '王%' 以及Username like '王_',%用来匹配任意字符,_用来匹配一个字符。而且还要注意的是,c语言中 %的转义方式是%%,而不是\%

上面一句话是从csdn上问的结果,插入的数据,是按照ID号从小的开始插入。也就是将中间的空缺补上

#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define _DEBUG_

int main( voID )
{
sqlite3 *db=NulL;
char *zErrMsg = 0;

int rc;

///open(create) database
rc = sqlite3_open("phonebook.db",&db);
if( rc )
{
fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return 0;
}
else printf("You have opened a sqlite3 database named zIEckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");

///create table
char *sql = " CREATE table Call_Record(\
Status CHAR(10),\
Username VARCHAR(20),\
Phonenubmer VARCHAR(20),\
Time timestamp,\
PRIMARY KEY (Status,Username)\
);" ;

//上面一句用来定义两个主键
sqlite3_exec( db,&zErrMsg );

///create table2
sql = " CREATE table Phonebook(\
Username VARCHAR(20) PRIMARY KEY,\
Phonenubmer VARCHAR(20)\
);" ;
sqlite3_exec( db,&zErrMsg );


///insert database
sql = "INSERT INTO \"Call_Record\" VALUES('missed','Tom','15908079860',datetime('Now','localtime'));" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Call_Record\" VALUES('answered','Jack','15908079861',&zErrMsg );

sql = "INSERT INTO \"Call_Record\" VALUES('dialed','Merry','15908079863','Zjf','15908079864','Qcx','15908079865','MM','15908079866','JJ','15908079867','GG','15908079868','DD','15908079869','Father','15908079870',&zErrMsg );

sql = "INSERT INTO \"Call_Record\" VALUES('missed','Mather','15908079871',&zErrMsg );

///insert database table two
sql = "INSERT INTO \"Phonebook\" VALUES('Tom','15908079860');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Jack','15908079861');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Merry','15908079863');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Zjf','15908079864');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Qcx','15908079865');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('MM','15908079866');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('JJ','15908079867');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('GG','15908079868');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('DD','15908079869');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Father','15908079870');" ;
sqlite3_exec( db,&zErrMsg );

sql = "INSERT INTO \"Phonebook\" VALUES('Mather','15908079871');" ;
sqlite3_exec( db,&zErrMsg );

int nrow = 0,ncolumn = 0;
char **azResult;

///select database
sql = "SELECT * FROM Call_Record ";

//sql = "select * from Phonebook order by Username"; //默认为升序asc

//sql = "select * from Phonebook order by Username desc"; //降序

//sql = "select * from Phonebook order by (case Username when \"\" then \'zzzzz\' else Username end);";

//sql = "select * from Phonebook order by (case Username when null then \'zzzzz\' else Username end);";

//上面一句是处理空值情况(null 与""),sqlserver默认空值为最小,使用case可以改变它查询的值。
sqlite3_get_table( db,&zErrMsg );
int i = 0 ;
printf( "row:%d column=%d \n",nrow,ncolumn );
printf( "\nThe result of querying is : \n" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s\n",i,azResult[i] );

///select database
sql = "SELECT * FROM Phonebook ";
sqlite3_get_table( db,&zErrMsg );
printf( "row:%d column=%d \n",azResult[i] );
/*
///delete database
sql = "DELETE FROM Call_Record WHERE Username = \'GG\' ;" ;

sqlite3_exec( db,&zErrMsg );

///update database
char sql1[100];
sprintf(sql1,"UPDATE Call_Record SET Phonenubmer = \'%s\' WHERE Username = \'%s\'","10111","MM");

sqlite3_exec( db,sql1,&zErrMsg );
*/
///select database desc (jiangxupailIE)
///if no desc is default(asc)
sql = "SELECT * FROM Call_Record order by Username desc";
sqlite3_get_table( db,&zErrMsg );
printf( "\n\n\n\nrow:%d column=%d ",ncolumn );
printf( "\nAfter deleting,the result of querying is : \n" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s\n",azResult[i] );


///free azResult
sqlite3_free_table( azResult );

///close db
sqlite3_close(db);
return 0;

}

另外一种查询获取列值的方法:

int i,j,rc,ncols;
char *sql;
char *tail;
char buff[100];
sqlite3_stmt *stmt;

///about dababase select
sql = "select * from Phonebook order by (case Username when \"\" then \'zzzzz\' else Username end);";
rc = sqlite3_prepare(db,&tail);
if (rc !=sqlITE_OK)
{
break;
}
rc = sqlite3_step(stmt); //取得数据表中一行的数据
ncols = sqlite3_column_count(stmt); //获取数据表有多少列
while(rc==sqlITE_ROW) {
for (j = 0; j < ncols; j++){
sprintf(buff,"%s",sqlite3_column_text(stmt,j)); //获取一行中每一列的数据
printf("%s\n",buff);
}
rc= sqlite3_step(stmt); //获取下一行数据
}

sqlite3_finalize(stmt);



http://hi.baidu.com/arise_xue/blog/item/56ec30ef9c28fde4cf1b3e9c.html/cmtid/56ec30ef244855e1cf1b3eff

总结

以上是内存溢出为你收集整理的sqlite3的sql语句用法总结与SQLite生成.db-journal文件问题全部内容,希望文章能够帮你解决sqlite3的sql语句用法总结与SQLite生成.db-journal文件问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176796.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存