C ++ sqlite database connection change operation check deletions

This code is then said last time, to use VS2013 to operate the database, we must first configure the environment, create a good database tables and so on.

I do not understand the previous two turn look ~ ~ ~

On the previous use of the goto statement, which I also refer to other bloggers to write, and now I commented out, goto, after all, we do not know, ha ha ha ~ ~ ~

Here to tell on my code, no accident, copy and paste can be used:

#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"  
#include "sqlite3.h"  
#include <iostream>  
#include <string> 
#include <Windows.h>
using namespace std;

sqlite3 * pDB = NULL;

//查找  
bool SelectUser();

//增加  
bool AddUser(const string& sName, const string& sFenshu);

//删除
bool DeleteUser(const  String & sName); 

// modify 
BOOL the ModifyUser ( const  String & sName, const  String & sFenshu); 

int the _tmain ( int argc, _TCHAR * the argv []) 
{ 
    // Open the path taken utf-8 encoded  
     // If the path contains Chinese, encoding conversion is needed   
    int nRes to sqlite3_open = ( " D: \\ \\ SQLite fuck.db; " , & PDB);
     IF (nRes =! SQLITE_OK) 
    { 
        COUT << " the Open Database Fail: " <<endl; 
        cost

    << " you delete " << endl; 
        cout << " 4. Modify " << endl; 
        cout << " 0. Exit " << endl; 
        cout << " *********** *************** C ++ sqlite connection data operation ************************************************************ " << endl ; 
        COUT << " Please select your desired operation " << endl; 
    } 
    
    int a;
     char name [ 20 is ];
    char fenshu [ 20 is ];
    the while ( to true ) 
    { 
        cin >> A;
         Switch (A) {
         Case  1 : 
            cout << " you choose the query " << endl; 
            SelectUser (); 
            cout << " *********** *************** Please continue to operate or select / exit ******************************************************** " < < endl;
             BREAK ;
         Case  2 : 
            cout << " you have chosen to add " << endl;
            cost<< "*******************请输入你要添加的信息如:xiaoming,18*******************" << endl;
            
            //cin >> id;
            cout << "name:" << endl;
            scanf("%s",&name);
            cout << "fenshu:" << endl;
            scanf("%s",&fenshu);
            //cout <<name  << fenshu << endl;
            if (AddUser(name,fenshu)){
            
                cout << "Added successfully! " << endl; 
                cout << " ************************** Please continue to operate or select / exit ******** ****************** " << endl; 
            } 
            BREAK ;
         Case  3 : 
            cout << " you have chosen to delete " << endl; 
            cout << " **** **************** Please enter your information to be deleted, such as: xiaoming ********************* " << endl;
             // delete   
            cout << " name: "
            scanf ( " % S " , & name);
             IF (DeleteUser (name)) 
            { 
                cout << " ! information deleted successfully " << endl; 
                cout << " ************** ************ Please continue to operate or select / exit ************************** " << endl; 
            } 

            BREAK ;
         Case  4 : 
            cout << " you have chosen to modify " << endl; 
            cout <<"Information changed successfully! " <<endl; 
                cout << " ************************** Please continue to operate or select / exit *********** *************** " << endl; 
            } 
            BREAK ;
         default : 
            cout << " you opted out " << endl; 
            cout << " Goodbye " << endl; 
            exit ( 0 ); 

        
        
        } 

    } 

    // find 
    / * IF (SelectUser (!)) 
    { 
        
        GOTO the QUIT;
    }*/

QUIT:
    sqlite3_close(pDB);

    return 0;
}


static int UserResult(void *NotUsed, int argc, char **argv, char **azColName)
{

    for (int i = 0; i < argc; i++)
    {
        
        cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << ", ";
    }
    //sqlite3_free_table(azColName);
    cout << endl;
    return 0;
}

bool SelectUser()
{
    char* cErrMsg;
    
//    int res = sqlite3_exec(pDB, "select student.id, student.name,score.fenshu from student, score WHERE student.id= score.id;", UserResult, 0, &cErrMsg);
    int res = sqlite3_exec(pDB, "select * from score", UserResult, 0, &cErrMsg);

    if (res != SQLITE_OK)
    {
        cout << "select fail: " << cErrMsg << endl;
        return false;
    }

    return true;
}

//添加数据
bool AddUser( const string& sName, const string& sFenshu)
{
    string strSql = "";
    strSql += "insert into score(name,fenshu)";
    strSql += "values('";
    strSql += sName;
    strSql += "',";
    strSql += sFenshu;
    strSql += ");";

    char* cErrMsg;
    int nRes = sqlite3_exec(pDB, strSql.c_str(), 0, 0, &cErrMsg);
    if (nRes != SQLITE_OK)
    {
        cout << "add score fail: " << cErrMsg << endl;
        return false;
    }
    else
    {
        cout << "add score success: " << sName.c_str() << "\t" << sFenshu.c_str() << endl;
    }

    return true;
}

//删除数据
bool DeleteUser(const string& sName)
{
    string strSql = "";
    strSql += "delete from score where name='";
    strSql += sName;
    strSql += "';";

    char* cErrMsg;
    int nRes = sqlite3_exec(pDB, strSql.c_str(), 0, 0, &cErrMsg);
    if (nRes != SQLITE_OK)
    {
        cout << "delete score fail: " << cErrMsg << endl;
        return false;
    }
    else
    {
        cout << "delete score success: " << sName.c_str() << endl;
    }

    return true;
}

//修改数据
bool ModifyUser(const string& sName, const string& sFenshu)
{
    string strSql = "";
    strSql += "update score set fenshu =";
    strSql += sFenshu;
    strSql += " where name='";
    strSql += sName;
    strSql += "';";

    char* cErrMsg;
    int nRes = sqlite3_exec(pDB, strSql.c_str(), 0, 0, &cErrMsg);
    if (nRes != SQLITE_OK)
    {
        cout << "modify score fail: " << cErrMsg << endl;
        return false;
    }
    else
    {
        cout << "modify score success: " << sName.c_str() << "\t" << sFenshu.c_str() << endl;
    }

    return true;
}

Description: score table is created in my own fuck.db, you can create your own, created earlier tutorials have ~~

Running interface:

 

Guess you like

Origin www.cnblogs.com/maoye520/p/11236351.html