博客來網路書店查詢

書名

博客來網路書店查詢

星期五, 5月 27, 2016

C++與SQLite--新增資料

student getStudent()
{
    student stud;
    int id;
    string name;
    float gpa;
    cout << " 輸入學號 :" ;
    cin >> id;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"學號格式錯了";
    }
    cout << " 輸入姓名 : " ;
    cin >> name;
    cout << " 輸入平均成績 : " ;
    cin >> gpa;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"成績格式錯了";
    }
    stud.setid(id);
    stud.setName(name);
    stud.setcgpa(gpa);
    return stud;
}
int addDataRow()
{
    student stud = getStudent();
    //cout << stud.getName() << endl;
    std::stringstream strm;
    strm << "insert into student(roll,name,cgpa) values(" << stud.getid() << ",'" << stud.getName() << "'," << stud.getcgpa() << ")";
    string s = strm.str();
    char *str = &s[0];
    //cout << str << endl;
    sqlite3_stmt *statement;
    int result;
    //char *query="insert into student(roll,name,cgpa)values(4,'uuu',6.6)";
    char *query = str;
    {
        if(sqlite3_prepare(dbfile,query,-1,&statement,0)==SQLITE_OK)
        {
            int res=sqlite3_step(statement);
            result=res;
            sqlite3_finalize(statement);
        }
        return result;
    }
    return 0;
}

沒有留言: