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)
{
printf("表格共%d 记录!\n", nRow);
printf("表格共%d 列!\n", nColumn);
// 前两个字段为字段名 field0, field1, row[0][0], row[0][1], row[1][0], row[1][1] ... ... ....
// 是一维数组,不是二维数组,反正记着第0,第1列的值为字段名,然后才是字段值;
printf( "字段名|字段值\n");
printf( "%s | %s\n", dbResult[0], dbResult[1]);
printf("--------------------------------\n");
int index = nColumn; //字段值从index开始呀
//ati c922 open file wait 2 write
QString fileName ="c:\csdnArtidNCatid.txt";
QFile f( fileName );
f.open(QIODevice::WriteOnly );
QTextStream t(&f);
//end
for( int i = 1; i < nRow ; i++ )
{
for( int j = 0 ; j < nColumn; j++ )
{
index=i*nColumn+j;
qDebug()<< dbResult[ index];
}
printf("\n");
QString arturl=dbResult[i*nColumn+0];
QStringList LIc922=arturl.split("/");
QString artID=LIc922[LIc922.length()-1];
QString catid=dbResult[i*nColumn+1];
QString lineC920=artID+","+catid;
t << lineC920 +"\r\n";
}
printf("--------------------------------\n");
f.close();
}
// char *ll=U2G(re[(2+1)*col+1]);
// cout<<ll<<endl;
// help->closeDB();
}
4.sqlite的记录结果稍微有点儿不一样..
-----------------------------------
是一纬的.. Fieldindex=rowINdex*nColumn+colIndex; z这样才能 查询到对应的记录..
参考:
C++操作SQLite数据库 - OPEN 开发经验库.htm
sqlite3_get_table使用举例 - babybandf的日志 - 网易博客.htm