#define ERR_MAG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
int insert_into(sqlite3 *sqldb);
int find_by_uname(sqlite3 *sqldb, char *uname);
int find_all(sqlite3 *sqldb);
int main(int argc, const char *argv[])
if (sqlite3_open(DB, &sqldb) != SQLITE_OK)
fprintf(stderr, "数据库打开错误 : %d : %s\t__%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
char *create_table = "CREATE TABLE if not exists register(uname char,passwd char)";
if (sqlite3_exec(sqldb, create_table, NULL, NULL, &err_msg) != SQLITE_OK)
fprintf(stderr, "create table : %d : %s : __%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
printf("---------------------------------------\n");
printf("------------1. 用户注册----------------\n");
printf("------------5. 查询全部信息-------------\n");
printf("------------6. 退出--------------------\n");
printf("---------------------------------------\n");
printf("choose = %c\n", choose);
if (sqlite3_close(sqldb) != SQLITE_OK)
fprintf(stderr, "close database : %d : %s : __%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
int insert_into(sqlite3 *sqldb)
printf("请输入准备注册的用户名 >>> ");
fgets(uname, sizeof(uname), stdin);
uname[strlen(uname) - 1] = 0;
fgets(passwd, sizeof(passwd), stdin);
passwd[strlen(passwd) - 1] = 0;
while (find_by_uname(sqldb, uname))
printf("请重新输入准备注册的用户名,输入exit取消注册 >>> ");
fgets(uname, sizeof(uname), stdin);
uname[strlen(uname) - 1] = 0;
if (!strcmp(uname, "exit"))
sprintf(sql_str, "insert into register (uname,passwd) values('%s','%s');", uname, passwd);
printf("sql_str = %s\n", sql_str);
if (sqlite3_exec(sqldb, sql_str, NULL, NULL, &errmsg) != SQLITE_OK)
fprintf(stderr, "insert into register : %d : %s __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
printf("用户 [%s] 注册成功\n", uname);
int find_by_uname(sqlite3 *sqldb, char *uname)
sprintf(sql_str, "select * from register where uname='%s';", uname);
if (sqlite3_get_table(sqldb, sql_str, &pres, &row, &colum, &errmsg) != SQLITE_OK)
fprintf(stderr, "find_by_uname error : %d : %s : __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
int find_all(sqlite3 *sqldb)
sprintf(sql_str, "select * from register;");
if (sqlite3_get_table(sqldb, sql_str, &pres, &row, &colum, &errmsg) != SQLITE_OK)
fprintf(stderr, "find_by_uname error : %d : %s : __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
for (int i = 0; i <= row; i++)
for (int j = 0; j < colum; j++)
printf("%s\t", pres[(row * i) + j]);