不知道如何获取数据库?
我是sqlite和gtk的初学者。我正在用code :: blocks创建我的第一个项目。
我在获取数据库时遇到问题,我的代码是:
I'm complete beginner with sqlite and gtk. I'm making my 1st project in code::blocks. I have a problem in fetching the database my code is:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "sqlite3.c"
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char *argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
/* Open database */
rc = sqlite3_open("sign_in.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
GtkWidget *window;
GtkWidget *table;
GdkColor color;
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *label3;
GtkWidget *entry1;
GtkWidget *entry2;
GtkWidget *button1;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "LOG IN");
gtk_container_set_border_width(GTK_CONTAINER(window), 30);
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
table = gtk_table_new(3, 2, FALSE);
gtk_container_add(GTK_CONTAINER(window), table);
label1 = gtk_label_new("---------------------------------- LOGIN AND ENJOY OUR FACILITY -----------------------------------");
label2 = gtk_label_new("USERNAME :");
label3= gtk_label_new("PASSWORD :");
button1 = gtk_button_new_with_label("LOG IN");
gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1,
GTK_FILL |GTK_EXPAND | GTK_SHRINK, GTK_FILL |GTK_EXPAND | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2,
GTK_FILL | GTK_EXPAND |GTK_SHRINK, GTK_FILL | GTK_EXPAND |GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 2, 3,
GTK_FILL | GTK_EXPAND |GTK_SHRINK, GTK_FILL | GTK_EXPAND |GTK_SHRINK, 5, 5);
entry1 = gtk_entry_new();
entry2 = gtk_entry_new();
gtk_table_attach(GTK_TABLE(table), entry1, 1, 2, 1, 2,
GTK_FILL |GTK_EXPAND | GTK_SHRINK, GTK_FILL |GTK_EXPAND | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), entry2, 1, 2, 2, 3,
GTK_FILL |GTK_EXPAND | GTK_SHRINK, GTK_FILL |GTK_EXPAND | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), button1, 1, 2, 3, 4,
GTK_FILL |GTK_EXPAND | GTK_SHRINK, GTK_FILL |GTK_EXPAND | GTK_SHRINK, 5, 5);
gtk_widget_show(table);
gtk_widget_show(label1);
gtk_widget_show(label2);
gtk_widget_show(label3);
gtk_widget_show(entry1);
gtk_widget_show(entry2);
gtk_widget_show(button1);
gtk_widget_show(window);
/* Create SQL statement */
sql="SELECT * from user where username='char *entry1' and password='char *entry2 '";
if(sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg))
{
int count=0;
while(sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg)+1)
{
count++;
}
if(count==1)
fprintf(stdout, "username and password is correct\n");
if(count>1)
fprintf(stdout, "Dulitcate user\n");
if(count<1)
fprintf(stdout, "username and password is not correct\n");
}
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);
g_signal_connect(G_OBJECT(button1), "clicked",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
我在这些行中面临问题(来自上面的代码):
I'm facing problem in these line (from above code):
- 在SELECT语句中(我知道它完全错误,请更正)。
-
在sqlite3_exec(db,sql ,回调,(void *)数据和& zErrMsg)++(在while循环中)
- In SELECT statement( i know its totally wrong ,please correct it).
In sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg)++ (in while loop)
/* Create SQL statement */
sql="SELECT * from user where username='char *entry1' and password='char *entry2 '";
if(sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg))
{
int count=0;
while(sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg)++)
{
count++;
}
if(count==1)
fprintf(stdout, "username and password is correct\n");
if(count>1)
fprintf(stdout, "Dulitcate user\n");
if(count<1)
fprintf(stdout, "username and password is not correct\n");
}
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);
请帮助我。我真的需要别人帮助。谢谢
please help me.i'really need someone help.thanx in advance
sqlite3_exec
不适合在查询的地方处理返回的数据,还有其他一些缺点。
sqlite3_exec
is not suitable if you want to handle the returned data at the place where you are querying it, and has some other disadvantages.
对于查询,您应始终使用 sqlite3_prepare_v2 ,然后调用 sqlite3_step 循环。
要将变量值放入语句中,请使用参数标记(?
)和 sqlite3_bind * 函数。
要读取返回的值,请使用 sqlite3_column_ * 函数(但是在这种情况下,实际上并不需要读取任何值。)
For a query, you should always use sqlite3_prepare_v2, and then call sqlite3_step in a loop.
To get variable values into the statement, use parameter markers (?
) and the sqlite3_bind* functions.
To read returned values, use the sqlite3_column_* functions (but in this case, you don't actually want to read any values).
char *user = "Supu";
char *password = "secret";
sqlite3_stmt *stmt;
const char *sql = "SELECT username, password FROM user WHERE username = ? AND password = ?";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "error: %s, %s\n", sql, sqlite3_errmsg(db));
} else {
sqlite3_bind_text(stmt, 1, user, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, password, -1, SQLITE_TRANSIENT);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
printf("returned row: user = %s, password = %s\n",
sqlite3_column_text(stmt, 0),
sqlite3_column_text(stmt, 1));
count++;
}
if (rc != SQLITE_DONE)
fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
}