SQLite基本操作
来源:互联网 发布:李小龙身材数据 编辑:程序博客网 时间:2024/04/29 13:24
#include "..\sqlite3\include\sqlite3.h"#include <assert.h>#include <iostream>#define SQLITE_SAFE_CLOSE( Ptr ){ if( nullptr != Ptr ){ sqlite3_close( Ptr ); Ptr = nullptr; } } #define SQLITE_SAFE_FREE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free( Ptr ); Ptr = nullptr; } } #define SQLITE_SAFE_FREE_TABLE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free_table( Ptr ); Ptr = nullptr; } } //回调查询int SelectTableCallBack( void* Param, int Column, char** ColumnValue, char** ColumnName ){ //注意每查询到一行会执行这个方法! //传入进来的参数赋值为true表示查询到数据 *static_cast< bool* >( Param ) = true; //这里字段名称和字段值是同步的,注意与非回调查询方式的区别! for( int i = 0; i < Column; ++i ) { printf( "column:%s\nvalues:%s\n", ColumnName[ i ], ColumnValue[ i ] ); } return 0;}int main(){ sqlite3* DataBase = nullptr; //打开或者创建数据库 if( SQLITE_OK != sqlite3_open( "test.db", &DataBase ) ) { assert( false ); return 0; } //错误消息 char* ErrorMsg = nullptr; do { //删除表 if( SQLITE_OK != sqlite3_exec( DataBase, "drop table test", nullptr, nullptr, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } //创建表 if( SQLITE_OK != sqlite3_exec( DataBase, "create table test( id integer primary key autoincrement, name nvarchar( 32 ) )", nullptr, nullptr, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } //插入数据 if( SQLITE_OK != sqlite3_exec( DataBase, "insert into test( name ) values( '张三' )", nullptr, nullptr, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } //插入数据 if( SQLITE_OK != sqlite3_exec( DataBase, "insert into test( name ) values( '李四' )", nullptr, nullptr, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } //修改表 if( SQLITE_OK != sqlite3_exec( DataBase, "update test set name = '李冰冰' where name = '李四'", nullptr, nullptr, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } bool IsSelectData = false; //回调方式查询表 if( SQLITE_OK != sqlite3_exec( DataBase, "select * from test", SelectTableCallBack, &IsSelectData, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } printf( "-------------------------------\n" ); //非回调方式查询表 char** Result = nullptr; int RowCount = 0; int ColumnCount = 0; if( SQLITE_OK != sqlite3_get_table( DataBase, "select * from test where name = '李冰冰'", &Result, &RowCount, &ColumnCount, &ErrorMsg ) ) { printf( "[%s]\n", ErrorMsg ); assert( false ); break; } IsSelectData = RowCount > 0; //Result[ 0 至 ColumnCount -1 ] 存的为字段名称,过后存的才是字段的值 for( int i = 0; i < RowCount; ++i ) { for( int j = 0; j < ColumnCount; ++j ) { printf( "column:%s\nvalues:%s\n", Result[ j ], Result[ ColumnCount + i * RowCount + j ] ); } } //释放掉表数据 SQLITE_SAFE_FREE_TABLE( Result ); if( !IsSelectData ) { printf( "没有查询到数据!\n" ); } }while( 0 ); //释放掉数据库 SQLITE_SAFE_CLOSE( DataBase ); //基本操作就是这样,可以满足绝大部分的功能,但是用起来还是不爽,大家试着封装成类 //以后我也会把封装好的传上来供大家参考 return 0;}
0 0
- sqlite数据库基本操作
- android---sqlite基本操作
- sqlite 基本操作指令
- SQLite数据库基本操作
- Android SQLite基本操作
- sqlite 基本操作指令
- SQLite常用基本操作
- Sqlite的基本操作
- SQLite 入门, 基本操作
- SQLite基本操作
- SQLite基本操作
- sqlite 基本操作
- sqlite基本操作
- sqlite基本操作命令
- SQLite基本操作
- Sqlite的基本操作
- sqlite基本操作补充
- sqlite数据库基本操作
- JS中的load方法
- TOMCAT-SSL双向认证-配置实例
- 数组对象(1)
- 寻找必败态——一类博弈问题的快速解法
- jquery中this与$(this)的用法区别.
- SQLite基本操作
- java远程调用linux的命令或者脚本
- 五大常用算法之一:分治算法
- Github上的iOS开源类库
- ZZ: 55分钟学会正则表达式
- 理解ThreadPoolExecutor源码(一)线程池的corePoolSize、maximumPoolSize和poolSize
- 五大常用算法之二:动态规划算法
- 查看哪些session在使用temp表空间
- android权限总汇