cpp: SQLite

 

 

 

lib /def:sqlite3.def /machine:ix86

 

 

 

 

 

 

 

 

 

 

 

如果程序文件编码是ANSI时还需要解决中文读写乱码问题

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
sqlite3* sql = NULL; // 一个打开的数据库实例
    const char* path = "geovindu";//某个sql文件的路径
 
    // 根据文件路径打开数据库连接。如果数据库不存在,则创建。
    // 数据库文件的路径必须以C字符串传入。
    int result = sqlite3_open_v2(path, &sql, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE, NULL);
 
    if (result == SQLITE_OK) {
        std::clog << "打开数据库连接成功\n";
    }
    else {
        std::clog << "打开数据库连接失败\n";
    }
 
 
    sqlite3_stmt* stmt = NULL;        //stmt语句句柄  和文件编码有关系
    //添加 插入中文时,需要转换
    /**/
 
    // insert_data(00010, "geovindu", const_cast<char *>(Unicode2Utf8(_T("测测测测测")).c_str()));
    //string name=Unicode2Utf8(_T("科学")).c_str();
    const char* sqlSentenceInsert = "INSERT INTO BookKindList(BookKindName,BookKindCode,BookKindParent) VALUES('涂聚文', 'C0012',1);";        //SQL语句
     
 
    //进行插入前的准备工作——检查语句合法性
    //-1代表系统会自动计算SQL语句的长度
    int resultInsert = sqlite3_prepare_v2(sql, sqlSentenceInsert, -1, &stmt, NULL);
 
    if (resultInsert == SQLITE_OK) {
        std::clog << "添加数据语句OK\n";
        //执行该语句
        sqlite3_step(stmt);
    }
    else {
        std::clog << "添加数据语句有问题\n";
    }
    //清理语句句柄,准备执行下一个语句
    sqlite3_finalize(stmt);
     
 
    //查询
    const char* sqlSentence = "SELECT BookKindID,BookKindName,BookKindCode,BookKindParent FROM BookKindList;";    //SQL语句
    //sqlite3_stmt* stmt = NULL;    // stmt语句句柄
 
    //进行查询前的准备工作——检查语句合法性
    //-1代表系统会自动计算SQL语句的长度
    int resultSelect = sqlite3_prepare_v2(sql, sqlSentence, -1, &stmt, NULL);
    if (resultSelect == SQLITE_OK) {
        std::clog << "查询语句OK\n";
 
        std::string BookKindName, BookKindCode;
 
        // 每调一次sqlite3_step()函数,stmt语句句柄就会指向下一条记录
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 取出第0列字段的值
            int BookKindID = sqlite3_column_int(stmt, 0);
             
            // 取出第1列字段的值 const
            //const unsigned char* BookKindName2 =sqlite3_column_text(stmt, 1);
            char* a = (char*)sqlite3_column_text(stmt, 1);//取出的数据
            //string bookname = string(BookKindName);
            //BookKindName.append(char*)sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
            //2
            //const unsigned char* BookKindCode2 = sqlite3_column_text(stmt, 2);
            char* b = (char*)sqlite3_column_text(stmt,2);
            //BookKindCode.append((char*)sqlite3_column_blob(stmt, 2)   , sqlite3_column_bytes(stmt, 2));
         
            //char* du = Gb2Utf(a);
 
            string du = string(a);
            string du1 = string(b);
            //wstring reasltutf8 = UTF8ToUnicode(du); // 如果程序文件编码是ANSI 需要这样转换
            //wstring reasltutf9 = UTF8ToUnicode(du1);
            //wstring reasltutfname = UTF8ToUnicode(BookKindName);
            //wstring resaltutfcode = UTF8ToUnicode(BookKindCode);
            //locale loc("chs");
            //wcout.imbue(loc);
 
            //3
            int BookKindParent = sqlite3_column_int(stmt, 3);
 
            //wcout <<"du:"<< reasltutf8<<" ,Name:"<< reasltutfname << endl;
 
            //输出相关查询的数据
            //std::clog << "BookKindName = "<<UTF8ToUnicode(BookKindName) << ", BookKindID = " << BookKindID << ",BookKindCode=" << UTF8ToUnicode(BookKindCode) << ",BookKindParent="<< BookKindParent <<endl;
            std::cout << "BookKindName = " << du << ", BookKindID = " << BookKindID << ",BookKindCode=" << du1 << ",BookKindParent=" << BookKindParent << endl;
 
            //wcout.clear();
        }
    }
    else {
        std::clog << "查询语句有问题";
    }
    //清理语句句柄,准备执行下一个语句
    sqlite3_finalize(stmt);
    //
 
 
 
 
    //关闭数据库
    sqlite3_finalize(stmt);
    if (sql) {
        sqlite3_close_v2(sql);
        sql = nullptr;
    }

  

 

 

 

 

 

 

 

 vs 2022 管理权限执行程序设置

 

posted @   ®Geovin Du Dream Park™  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
历史上的今天:
2011-04-15 IE和firefox通用的复制到剪贴板的JS函数,Opera测试不成功!
2010-04-15 javascript检测浏览器类型和版本号(兼容各浏览器)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示