gpt4 book ai didi

c - 不知道如何获取数据库?

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:59 25 4
gpt4 key购买 nike

我是 sqlite 和 gtk 的初学者。我正在用 code::blocks 制作我的第一个项目。我在获取数据库时遇到问题,我的代码是:

#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);

我在这些行中遇到问题(来自上面的代码):

  1. 在SELECT语句中(我知道完全错误,请更正)。
  2. 在 sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg)++ (在 while 循环中)

    /* 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);

    请帮助我。我真的需要有人帮助。谢谢

最佳答案

sqlite3_exec 不适合在查询的地方处理返回的数据,还有其他一些缺点。

对于查询,您应该始终使用 sqlite3_prepare_v2 , 然后调用 sqlite3_step在一个循环中。要将变量值放入语句中,请使用参数标记 (?) 和 sqlite3_bind*功能。要读取返回值,请使用 sqlite3_column_*函数(但在这种情况下,您实际上并不想读取任何值)。

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);
}

关于c - 不知道如何获取数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com