问题已经解决,不是出在查询方面,是出在插入方面,不能直接用sqlite3_exec( db,sql,&zErrMsg );,使用了如下方式插入数据就正常了。
。。。
char *sql = "INSERT INTO /"ca01/" VALUES( '001',?,'1111111','222');" ;
sqlite3_stmt * stmt;
sqlite3_prepare ( db,strlen ( sql),&stmt,NULL );
sqlite3_bind_text(stmt,1,"深圳市宝安区爱华产品经销部",256,sqlITE_TRANSIENT);
sqlite3_step ( stmt );
。。。
非常感谢各位热心的朋友。
。。。
char *sql = "INSERT INTO /"ca01/" VALUES( '001',?,'1111111','222');" ;
sqlite3_stmt * stmt;
sqlite3_prepare ( db,strlen ( sql),&stmt,NULL );
sqlite3_bind_text(stmt,1,"深圳市宝安区爱华产品经销部",256,sqlITE_TRANSIENT);
sqlite3_step ( stmt );
。。。
非常感谢各位热心的朋友。
淡去的烟味
2007-11-2 19:02:21
tty1a兄弟: 我的代码如下,但还是只查到0条记录.
sqlite3 *db=NULL;
char *errmsg = 0;
int result;
result = sqlite3_open("my.db",&db);
char sz[] = "CREATE TABLE [ca01] ( [a] text NULL,[b] text NULL,[c] text NULL,[d] text NULL)";
result = sqlite3_exec(db,sz,NULL,&errmsg);
char *sql = "INSERT INTO /"ca01/" VALUES( '001',sqlITE_TRANSIENT);
sqlite3_step ( stmt );
char **dbResult;
int nRow,nColumn,i,j,index;
result = sqlite3_get_table( db,"select * from ca01 WHERE b like '%%爱华%%' ",&dbResult,&nRow,&nColumn,&errmsg );
if ( sqlITE_OK == result )
{
index = nColumn;
TRACE( "查到%d条记录/n",nRow );
for( i = 0; i < nRow ; i++ )
{
TRACE( "第 %d 条记录/n",i+1 );
for( j = 0 ; j < nColumn; j++ )
{
TRACE("字段名:%s -> 字段值:%s/n",dbResult[j],dbResult [index] );
++index;
}
TRACE("-------/n" );
}
}
sqlite3_free_table( dbResult );
sqlite3_close(db);
sqlite3 *db=NULL;
char *errmsg = 0;
int result;
result = sqlite3_open("my.db",&db);
char sz[] = "CREATE TABLE [ca01] ( [a] text NULL,[b] text NULL,[c] text NULL,[d] text NULL)";
result = sqlite3_exec(db,sz,NULL,&errmsg);
char *sql = "INSERT INTO /"ca01/" VALUES( '001',sqlITE_TRANSIENT);
sqlite3_step ( stmt );
char **dbResult;
int nRow,nColumn,i,j,index;
result = sqlite3_get_table( db,"select * from ca01 WHERE b like '%%爱华%%' ",&dbResult,&nRow,&nColumn,&errmsg );
if ( sqlITE_OK == result )
{
index = nColumn;
TRACE( "查到%d条记录/n",nRow );
for( i = 0; i < nRow ; i++ )
{
TRACE( "第 %d 条记录/n",i+1 );
for( j = 0 ; j < nColumn; j++ )
{
TRACE("字段名:%s -> 字段值:%s/n",dbResult[j],dbResult [index] );
++index;
}
TRACE("-------/n" );
}
}
sqlite3_free_table( dbResult );
sqlite3_close(db);
2007-11-5 0:35:00
近日忙,没能及时回来深感抱歉,你查不到,估计问题出在使用了sqlite3_get_table来查,请试着使用绑定的方式。对于sqlite3_get_table要慎用甚至不用。
以下的方式可以查到:
sqlite3 *db=NULL;
int rc = sqlite3_open("data.db",&db ); //打开指定的数据库文件
if( rc )
{
ShowMessage( sqlite3_errmsg(db) );
sqlite3_close( db );
return;
}
sqlite3_stmt * stmt;
AnsiString temp = "%爱华%";
char * sqlstr = "SELECT * FROM ca01 where a002 like ?;";
sqlite3_prepare ( db,sqlstr,strlen ( sqlstr ),NULL );
sqlite3_bind_text( stmt,temp.c_str(),temp.Length(),sqlITE_TRANSIENT );
while ( sqlite3_step ( stmt )==sqlITE_ROW )
{
//测试
Edit1->Text = StrPas(sqlite3_column_text( stmt,0) );
Edit2->Text = StrPas(sqlite3_column_text( stmt,1) );
}
sqlite3_finalize ( stmt );
sqlite3_close( db );
以下的方式可以查到:
sqlite3 *db=NULL;
int rc = sqlite3_open("data.db",&db ); //打开指定的数据库文件
if( rc )
{
ShowMessage( sqlite3_errmsg(db) );
sqlite3_close( db );
return;
}
sqlite3_stmt * stmt;
AnsiString temp = "%爱华%";
char * sqlstr = "SELECT * FROM ca01 where a002 like ?;";
sqlite3_prepare ( db,sqlstr,strlen ( sqlstr ),NULL );
sqlite3_bind_text( stmt,temp.c_str(),temp.Length(),sqlITE_TRANSIENT );
while ( sqlite3_step ( stmt )==sqlITE_ROW )
{
//测试
Edit1->Text = StrPas(sqlite3_column_text( stmt,0) );
Edit2->Text = StrPas(sqlite3_column_text( stmt,1) );
}
sqlite3_finalize ( stmt );
sqlite3_close( db );
淡去的烟味
2007-11-5 22:44:37
tty1a兄弟,我照您的代码试下,但 sqlite3_step总是返回sqlITE_DONE,而不是sqlITE_ROW,所以总是不进while循环,就是查不到记录啊. 代码如下:
sqlite3 *db=NULL;
char *errmsg = 0;
int result;
result = sqlite3_open("my.db",&db);
char sz[] = "CREATE TABLE [ca01] ([a001] text NULL,[a002] text NULL,[a003] text NULL,[a004] text NULL)";
result = sqlite3_exec(db,sqlITE_TRANSIENT);
sqlite3_step ( stmt );
// AnsiString temp = "%爱华%";
char temp[100] = "'%爱华%'"; // 换成"%%爱华%%" 或 " '%%爱华%%' " 都不行
char * sqlstr = "SELECT * FROM ca01 where a002 like ?";
sqlite3_prepare ( db,NULL );
// sqlite3_bind_text( stmt,sqlITE_TRANSIENT );
sqlite3_bind_text( stmt,temp,strlen(temp),sqlITE_TRANSIENT );
while ( sqlite3_step ( stmt )==sqlITE_ROW ) // 总是sqlite3_step总是返回sqlITE_DONE,而不是sqlITE_ROW
{
//测试
printf( "%s,%s/n",sqlite3_column_text( stmt,0),1) );
// Edit1->Text = StrPas(sqlite3_column_text( stmt,0) );
// Edit2->Text = StrPas(sqlite3_column_text( stmt,1) );
}
sqlite3_finalize ( stmt );
sqlite3_close( db );
sqlite3 *db=NULL;
char *errmsg = 0;
int result;
result = sqlite3_open("my.db",&db);
char sz[] = "CREATE TABLE [ca01] ([a001] text NULL,[a002] text NULL,[a003] text NULL,[a004] text NULL)";
result = sqlite3_exec(db,sqlITE_TRANSIENT);
sqlite3_step ( stmt );
// AnsiString temp = "%爱华%";
char temp[100] = "'%爱华%'"; // 换成"%%爱华%%" 或 " '%%爱华%%' " 都不行
char * sqlstr = "SELECT * FROM ca01 where a002 like ?";
sqlite3_prepare ( db,NULL );
// sqlite3_bind_text( stmt,sqlITE_TRANSIENT );
sqlite3_bind_text( stmt,temp,strlen(temp),sqlITE_TRANSIENT );
while ( sqlite3_step ( stmt )==sqlITE_ROW ) // 总是sqlite3_step总是返回sqlITE_DONE,而不是sqlITE_ROW
{
//测试
printf( "%s,%s/n",sqlite3_column_text( stmt,0),1) );
// Edit1->Text = StrPas(sqlite3_column_text( stmt,0) );
// Edit2->Text = StrPas(sqlite3_column_text( stmt,1) );
}
sqlite3_finalize ( stmt );
sqlite3_close( db );
2007-11-6 0:39:42
那的确是很奇怪了,有可能是你编程环境的原因了,像有朋友说在C#上用得很好。我刚刚用您上面贴出的代码测试了一下,的确是没有问题啊,返回的结果是正确的。
注:测试环境: 简体中文WINXP + C++Build 6.0 + sqlite3
淡去的烟味
2007-11-6 3:26:31
AnsiString temp = "%爱华%";
或
char temp[100] = "'%爱华%'";
或
char temp[100] = "%%爱华%%";
都测试过,可以。
我原本觉得要真是用不了LIKE子句来查询数据那真是个大麻烦了,使用起来会非常被动。现在总算可以了。
不知道sqlite For Linux版能不能用到在sco openserver下,当然真正应用上估计要优先考虑在Linux上使用,只是因为装有现在的系统方便。等有时间测试一下。
[ 本帖子最后于 2007-11-6 3:55:27 编辑 ]
淡去的烟味
2007-11-6 3:46:47