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

本文总结了使用C++操作SQLite数据库的方法,包括引用DLL、创建helper类进行简化操作、数据库连接与关闭流程,以及如何执行SQL语句和查询数据。通过实例展示了如何将查询结果输出到文件中。
paip.c++ sqlite数据库操作总结




作者Attilax ,  EMAIL:1466519819@qq.com 
来源:attilax的专栏
地址:http://blog.youkuaiyun.com/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
03-12
### PAIP编程珠玑中的示例代码解释 PAIP(Paradigms of Artificial Intelligence Programming)是一本深入探讨人工智能编程范式的书籍,其中包含了大量 Lisp 编写的程序实例。这些例子不仅展示了如何实现特定的人工智能算法,还提供了关于良好软件工程实践的重要见解。 #### 示例:通用求解器框架 书中介绍了一个名为 `defun` 的宏来定义函数,在构建通用问题解决器时非常有用[^1]: ```lisp (defun solve (problem) "Find a solution to the given problem." (let ((solution nil)) ;; Attempt to find a solution using backtracking. (labels ((try-next-option () (when (not solution) (if (no-more-options-p ()) (return-from try-next-option nil) (let* ((option (next-option))) (cond ((goal-reached-p option) (setf solution option)) (t (push-state option) (solve problem) (pop-state)))))))) (try-next-option) solution))) ``` 这段代码实现了回溯法解决问题的一般模式。通过递归调用自身并尝试不同的选项直到找到解决方案为止。如果当前路径无法通向目标,则会恢复之前的状态继续探索其他可能性。 此方法能够有效地处理许多复杂的组合优化类问题,并且由于其灵活性可以很容易地适应各种具体应用场景下的需求变化。 #### 示例:自然语言理解模块 另一个重要的部分涉及到了自然语言处理技术的应用案例——基于语法分析树结构来进行语义解析: ```lisp (defun parse-sentence (sentence) "Parse SENTENCE into its constituent parts and build an interpretation tree." (multiple-value-bind (subject verb-object) (split sentence 'verb) `(sentence :subject ,(parse-noun-phrase subject) :action ,verb-object))) (defun parse-noun-phrase (np-string) "Interpret NP-STRING as either a simple noun or compound phrase." ...) ``` 上述片段演示了如何将输入字符串分割成主谓宾成分,并进一步解析名词短语的具体含义。这种层次化的表示方式有助于后续更高级别的推理操作以及对话管理等功能的设计与实现。 #### 示例:专家系统规则引擎 最后值得一提的是书中对于专家系统的讨论,特别是有关于事实库管理和匹配机制的部分: ```lisp (defstruct fact id pattern bindings) (defun match-patterns (pattern facts) "Return all FACTS that unify with PATTERN, along with their BINDINGS." ...) (defun add-fact (kb new-fact) "Add NEW-FACT to knowledge base KB after checking consistency against existing rules." ...) ``` 这里展示了一种简单而有效的知识表达形式及其相应的查询接口设计思路。通过对模式进行统一化计算从而快速定位符合条件的事实条目;而在更新数据库前则需确保新加入的信息不会引起逻辑矛盾等问题的发生。 以上仅是从《Programming Pearls》一书摘取的一些精彩片段,实际上该著作涵盖了更为广泛的内容领域和技术细节等待读者去发掘学习。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值