发信息

#include "sqlite3.h"
#include <iostream>
using namespace std;
void cls(){
    system("cls");
}
void list(){
    system("cls");
    cout<<"-------聊天室--------"<<endl;
    cout<<"---1.清空聊天内容----"<<endl;
    cout<<"---2.退出聊天室------"<<endl;
    cout<<"---3.发送聊天信息----"<<endl;
    cout<<"---4.读取聊天内容----"<<endl;
    cout<<"---5.输出帮助列表----"<<endl;
}
int main() {
    sqlite3* db;
    char *errorMessage = 0;
    sqlite3_stmt* stmt;
    int rc;
    rc = sqlite3_open("database.db", &db); // 替换为你的数据库文件路径
    if (rc != SQLITE_OK) {
        std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
        sqlite3_close(db);
        return 1;
    }
    
    int n;
    cls();
    list();
    while(1){
        cin>>n;
        if(n==1){
            cls();
        }
        if(n==2){
            break;
        }
        if(n==3){
        
            string sql = "INSERT INTO chat VALUES ("+", 'liujiaxing', '7');";
    
            rc = sqlite3_exec(db, sql.c_str(), 0, 0, &errorMessage);
            if (rc != SQLITE_OK)
            {
                cout << "SQL错误: " << errorMessage << endl;
                sqlite3_free(errorMessage);
            }
        }
        if(n==4){
            const char* sql = "SELECT id, name, text FROM chat;"; // 替换为你的SQL查询
            rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
            if (rc != SQLITE_OK) {
                std::cerr << "Failed to prepare statement: " << sqlite3_errmsg(db) << std::endl;
                sqlite3_close(db);
                return 1;
            }
            while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
                cls();
                std::cout << sqlite3_column_text(stmt, 1)<<"说:"<< sqlite3_column_text(stmt, 2) << std::endl;
            }
        }
        if(n==5){
            list();
        }
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return 0;
}
/*;


    
    int rc;
    rc = sqlite3_open("database.db", &db);
    if (rc)
    {
        cout << "无法打开数据库: " << sqlite3_errmsg(db) << endl;
        return 0;
    }
    else
    {
        cout << "成功打开数据库" << endl;
    }
    // 执行插入
    //INSERT INTO "main"."chengji" ("id", "name", "grade", "class", "xingbie", "yuwen", "shuxue", "yingyu", "zhengzhi", "lishi") VALUES (8, '刘佳兴', '七年级', '9', '男', 99.0, 117.0, 116.0, 64.0, 43.0);
    //INSERT INTO chengji VALUES (8, '刘佳兴', '七年级', '9', '男', 99.0, 117.0, 116.0, 64.0, 43.0);

    //  
    cout << "记录插入成功" << endl;
*/
 
    
 
    
 

 

posted @ 2024-12-14 16:05  -刘-j-x-  阅读(18)  评论(0)    收藏  举报