1.SQLite是一种嵌入式关系型数据库管理系统,它是在零配置和服务器环境下的自包含库中实现的,优点是轻量级、高效和易于使用。可查阅SQLite的文档和示例代码来了解更多用法和功能。An Introduction To The SQLite C/C++ Interface
2.SQLiteStudio是一个开源的SQLite数据库管理工具,它提供了一个直观的图形界面,用于浏览、查询和管理SQLite数据库。
3.VS2022,x86,以登录注册界面为例。
(1)搭建环境如下。
(2)代码如下。
#include<iostream>
#include"sqlite3.h" //use sqlite
#include<graphics.h> //use esayX
#include<conio.h> //use _getch
#include<thread> //use thread
#include <sstream> //use stringstream
using namespace std;
int flag = 0; //输入框标志,0不输入,1输入账号,2输入密码
bool loginSuccess = false; //登录成功标志
char username[20]; //用户名
char password[20]; //密码
//按钮
void drawButton(int x, int y, int w, int h, const char* text)
{
setfillcolor(LIGHTBLUE);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(20, 0, _T("黑体"), 0, 0, 800, 0, 0, 0);
settextcolor(WHITE);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}
//处理输入
void drawLogin()
{
int i1 = 0;
int i2 = 0;
char star[20];
char ch;
while (ch = _getch())
{
if (flag == 1) //输入账号
{
if ((int)ch == 8) //Backspace键值
{
if (i1 == 0)
continue;
--i1;
username[i1] = '\0';
}
else
{
username[i1] = ch;
i1++;
username[i1] = '\0';
}
clearrectangle(150, 350, 400, 400);
settextcolor(WHITE);
outtextxy(150, 350, username);
}
else if (flag == 2) //输入密码
{
if ((int)ch == 8)
{
if (i2 == 0)
continue;
--i2;
password[i2] = '\0';
star[i2] = '\0';
}
else
{
password[i2] = ch;
star[i2] = '*';
i2++;
password[i2] = '\0';
star[i2] = '\0';
}
clearrectangle(150, 450, 400, 500);
settextcolor(WHITE);
outtextxy(150, 450, star);
}
}
}
//查询的回调函数
int callback(void* data, int argc, char** argv, char** colNames)
{
loginSuccess = true;
return 0;
}
int main(int argc)
{
//界面初始化
initgraph(1400, 800);
HWND hnd = GetHWnd();
SetWindowText(hnd, "廉政审核中腐败廉洁倾向性指数智能测试系统");
IMAGE imageLogin;
loadimage(&imageLogin, "123.jpg", 1400, 800, true);
IMAGE imageRun;
loadimage(&imageRun, "1234.jpg", 1400, 800, true);
putimage(0, 0, &imageLogin);
setbkmode(TRANSPARENT);
drawButton(150,600, 100, 50, "登录");
drawButton(300, 600, 100, 50, "注册");
settextstyle(20, 0, "黑体", 0, 0, 1000, 0, 0, 0);
settextcolor(LIGHTBLUE);
RECT r1 = { 100, 350, 150,400 };
drawtext("账号", &r1, DT_LEFT);
RECT r2 = { 100, 450, 150, 500 };
drawtext("密码", &r2, DT_LEFT);
setfillcolor(0xFACE87);
fillrectangle(0,700, 1400, 800);
RECT r3 = { 500,700, 1350, 750 };
drawtext("辽宁大学 地址:沈阳市皇姑区崇山中路66号", &r3, DT_LEFT);
RECT r4 = { 500, 750, 1350, 800 };
drawtext("邮编:110036", &r4, DT_LEFT);
setfillcolor(LIGHTBLUE);
fillrectangle(150,350, 400,400);
fillrectangle(150, 450, 400,500);
setbkcolor(LIGHTBLUE);
//开一个子线程给输入框, 因为处理输入和处理鼠标事件都是循环。
thread t(drawLogin);
t.detach();
//数据库初始化
sqlite3* db;
int rc = -1;
char* errorMsg = nullptr;
const char* sql;
//打开或创建一个数据库连接
rc = sqlite3_open("user.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}
else {
fprintf(stderr, "Opened database successfully\n");
}
//创建表
sql = "CREATE TABLE USERTABLE(" \
"UserName TEXT UNIQUE NOT NULL," \
"PassWord CHAR(20) NOT NULL);";
rc = sqlite3_exec(db, sql, NULL, 0, &errorMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errorMsg);
sqlite3_free(errorMsg);
}
else {
fprintf(stdout, "Table created successfully\n");
}
//处理鼠标事件
MOUSEMSG mouse;
while (1)
{
mouse = GetMouseMsg();
if (WM_LBUTTONDOWN == mouse.uMsg)
{
int x = (mouse.x + 20) / 40 * 40;
int y = (mouse.y + 20) / 40 * 40;
if (x >= 150 && x <= 400 && y >= 350 && y <= 400)//账号输入框
{
flag = 1;
}
else if (x >= 150 && x <= 400 && y >= 450 && y <= 500)//密码输入框
{
flag = 2;
}
else if (x >= 150 && x <= 250 && y >= 600 && y <= 650)//登录
{
flag = 0;
//查询
stringstream ss;
ss << "SELECT * FROM USERTABLE WHERE UserName = '";
ss << username;
ss << "' AND PassWord = '";
ss << password;
ss << "';";
rc = sqlite3_exec(db, ss.str().data(), callback, 0, &errorMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errorMsg);
sqlite3_free(errorMsg);
}
if (loginSuccess)
{
fprintf(stdout, "Operation done successfully\n");
putimage(0, 0, &imageRun);
settextstyle(30, 0, "黑体", 0, 0, 800, 0, 0, 0);
settextcolor(BLACK);
RECT r1 = { 600, 400, 900, 500 };
drawtext("登录成功后的界面...", &r1, DT_LEFT | DT_WORDBREAK);
break;
}
else {
MessageBox(GetHWnd(), "登录失败", "提示", MB_OK);
}
}
else if (x >= 300 && x <= 400 && y >= 600 && y <= 650)//注册
{
flag = 0;
//插入
stringstream ss;
ss << "INSERT INTO USERTABLE (UserName,PassWord) VALUES ('";
ss << username;
ss << "',";
ss << password;
ss << ");";
rc = sqlite3_exec(db, ss.str().data(), NULL, 0, &errorMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errorMsg);
sqlite3_free(errorMsg);
MessageBox(GetHWnd(), "注册失败", "提示", MB_OK);
}
else {
fprintf(stdout, "Records created successfully\n");
MessageBox(GetHWnd(), "注册成功", "提示", MB_OK);
}
}
}
}
//关闭数据库
sqlite3_close(db);
system("pause");
closegraph();
return 0;
}
(3)结果如下。
输入错误时弹窗提示。
输入正确时切换界面。
数据库可视化,用SQLiteStudio打开user.db。
4.注意,查询语句分三种情况:第一种查询出错,返回错误代码;第二种查询成功,且查询到数据执行回调函数,返回0;第三种查询成功,但查询不到数据,返回0。因此,需注意处理。