日期:2014-05-16  浏览次数:20632 次

paip.c++ sqlite数据库操作总结
paip.c++ sqlite数据库操作总结




作者Attilax ,  EMAIL:1466519819@qq.com 
来源:attilax的专栏
地址:http://blog.csdn.net/attilax




1.引用sqlite3.dll
-------------------------


INCLUDEPATH +=  D:\sqlitelib
LIBS += D:\sqlitelib\sqlite3.dll


2.为了方便,写了个helper类..
---------------------------


--------*.h--------
#ifndef SQLITEHELPER_H
#define SQLITEHELPER_H




#pragma once


#include "sqlite3.h"


class SQLiteHelper
{
public:
    SQLiteHelper();
    virtual ~SQLiteHelper();
    sqlite3 *db;
    void execSQL(char *sql);
    char**rawQuery(char *sql,int *row,int *column,char **result);
    void openDB(char *path);
    void closeDB();










};






#endif // SQLITEHELPER_H


----cpp---------
#include "sqlitehelper.h"
 #include  <iostream>
using namespace std;


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


SQLiteHelper::SQLiteHelper()
{


}


SQLiteHelper::~SQLiteHelper()
{


}
void SQLiteHelper::execSQL(char *sql)
{
    sqlite3_exec(db,sql,0,0,0);
}
char **SQLiteHelper::rawQuery(char *sql,int *row,int *column,char **result)
{
    sqlite3_get_table(db,sql,&result,row,column,0);
    return result;
}
void SQLiteHelper::openDB(char *path)
{
    int last=sqlite3_open(path,&db);
    if(SQLITE_OK!=last)
    {
        cout<<"打开数据库出错"<<endl;
    }
  //  sqlite3_close(db);
  //  sqlitehelper::closedb()
}






3.使用...
-----------------


#include <QCoreApplication>
 #include <QNetworkAccessManager>
#include <QtCore>
#include <QtNetwork>
 #include <QNetworkRequest>


 #include <QNetworkReply>


#include "atinet.h"
#include "atifile.h"
#include "sqlite3.h"
#include "sqlitehelper.h"
#include <tchar.h>
#include <string.h>
#include <iostream>
//#include "qnetworkaccessmanager.h"
//#include <qnetworkaccessmanager.h>
using namespace std ;
//static QString getHtml(QString url);
//QString getHtml2(QString url);
void testRegexCapture();
void exportArtidNCateid(QString dbpath,QString sql);


void exportArtidNCateid(QString dbpath,QString sql)
{


    SQLiteHelper *help=new SQLiteHelper();
    std::string dbpath3 = dbpath.toStdString();
     char* dbpath2= (char *)dbpath3.c_str();


    help->openDB(dbpath2);
//    char *sql="insert into dota values(6,'zhycheng')";
//    help->execSQL(sql);
    std::string str3 = sql.toStdString();
    char *sql2= (char *)str3.c_str();
    int nRow,nColumn;
    char *eee="i";
       char *errmsg;
    char **dbResult=&eee;
 //   char **re=help->rawQuery(sql2,&nRow,&nColumn,dbResult);
    int re=   sqlite3_get_table( help->db, sql2, &dbResult, &nRow, &nColumn, &errmsg);
    if (re == SQLITE_OK)
    &nbs