醍醐灌顶!多谢楼上!
我在程序中把这个错误改过来了,但是运行时依然提示“段错误”
程序源代码粘贴如下:
14 #include "sqlite3.h"
15 void teaform::on_okButtonClicked()
16 {
17 sqlite3* db;
18 char *num,*author,*storage;
19 int rc,nRow,nCol;
20 char **dbResult;
21 char *sql,*errmsg=NULL,*str;
22
23 QString nameText=nameEdit->text();
24 QString resultQString,errMsgQString;
25
26 //open the databse:
27 rc=sqlite3_open("library1.db",&db);
28
29 //open failed:
30 if(rc)
31 {
32 errMsgQString.sprintf("cannot open database: %s\n",sqlite3_errmsg(db));
33 resultEdit->setText(errMsgQString);
34 sqlite3_close(db);
35 }
36
37 //open successed, then do querying:
38 else{
39 strcpy(str,nameText.ascii());
40 sprintf(sql,"select * from book where BookName='%s';",str);
41
42 rc=sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&errmsg);
43
44 //get table failed:
45 if(rc!=SQLITE_OK)
46 {
47 resultQString.sprintf("cannot open database: %s\n",errmsg);
48 resultEdit->setText(resultQString);
49 sqlite3_free_table(dbResult);
50 sqlite3_close(db);
51 }
52
53 //get table successfully:
54 else
55 {
56 int j=4;
57 while(j!=8)
58 {
59 switch(j)
60 {
61 case 4: strcpy(num,dbResult[j]);
62 case 5:
63 case 6: strcpy(author,dbResult[j]);
64 case 7: strcpy(storage,dbResult[j]);
65 default: break;
66 }
67 j++;
68 }
69
70 resultQString.sprintf("%s,%s,%s\n",num,author,storage);
71 resultEdit->setText(resultQString);
72 sqlite3_free_table(dbResult);
73 sqlite3_close(db);
74 }
75 }
76 }
make时有警告信息:
.ui/../teaform.ui.h: In member function ‘virtual void teaform::on_okButtonClicked()’:
.ui/../teaform.ui.h:21: warning: ‘str’ may be used uninitialized in this function
.ui/../teaform.ui.h:21: warning: ‘sql’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘num’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘author’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘storage’ may be used uninitialized in this function
大概知道是定义的指针变量方面造成了“段错误”,但不知道该怎么改,希望大家帮我看看,本人C语言知识太贫乏,下面也会多学习。。。