sqlite3增删查改应用
应用 增删 sqlite3 查改
2023-09-27 14:29:23 时间
sqlite3数据库可以采用MesaSQLite可视化工具
2.添加sqlite3支持的库文件,libsqlite3.dylib
3.创建viewController控制器,布局好界面
![](http://img.blog.csdn.net/20130827090730375?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGluZ3hpYW93ZWkyMDEz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
ViewController.h:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * document = [path objectAtIndex:0]; return [document stringByAppendingPathComponent:kFilename]; - (IBAction)addClick:(id)sender { //创建 sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); char * update = "insert or replace into FIELDS (id, name)" "values (?,?);"; sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, update, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 sqlite3_bind_text(stmt, 2, [self.nameText.text UTF8String], -1, NULL);//2表示第二个问号 if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error updating table:%s",errorMsg); sqlite3_finalize(stmt); //这句话不能漏 sqlite3_close(database); //关闭数据库 UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"添加成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)deleteClick:(id)sender { sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg);
sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, del, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error delete table:%s",errorMsg); sqlite3_finalize(stmt); sqlite3_close(database); UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"删除成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)updateClick:(id)sender { sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); char * update = "update FIELDS set id = ?,name = ? where id = ?"; sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, update, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 sqlite3_bind_text(stmt, 2, [self.nameText.text UTF8String], -1, NULL);//2表示第二个问号 sqlite3_bind_int(stmt, 3, [self.idText.text intValue]); if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error updating table:%s",errorMsg); sqlite3_finalize(stmt); //这句话不能漏 sqlite3_close(database); //关闭数据库 UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"修改成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)selectClick:(id)sender { //创建数据库 sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); NSString * query = @"select * from FIELDS where id = ?"; sqlite3_stmt * statement; if(sqlite3_prepare_v2(database, [query UTF8String], -1, statement, nil) == SQLITE_OK) sqlite3_bind_int(statement, 1, [self.txtSearchBar.text intValue]); //sqlite3_bind_text(statement, 2, [self.nameText.text UTF8String], -1, NULL); while(sqlite3_step(statement) == SQLITE_ROW) int id = sqlite3_column_int(statement, 0); //0表示第1列 char * name = (char *)sqlite3_column_text(statement, 1); //1表示第2列 NSString * sid = [[NSString alloc] initWithFormat:@"%d",id]; NSString * sname = [[NSString alloc] initWithUTF8String:name]; self.nameText.text = sname; self.idText.text = sid; UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"查询成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; sqlite3_finalize(statement); else NSLog(@"error");
SQLite关于数据库和表的操作 SQLite 的 DETACH DATABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的。如果同一个数据库文件已经被附加上多个别名,DETACH 命令将只断开给定名称的连接,而其余的仍然有效。您无法分离 main 或 temp 数据库。 SQLite 的 DETACH DATABASE ‘Alias-Name’ 语句的基本语法如下:
SQLite编程时列出所有在数据库中创建的表 因为所有的点命令只在 SQLite 提示符中可用,所以当您进行带有 SQLite 的编程时,您要使用下面的带有 sqlite_master 表的 SELECT 语句来列出所有在数据库中创建的表:
FMDB | 实现数据的增删改查 FMDB是一个轻量级的数据库,用于将网络资源存储在本地。 项目中使用 ARC 还是 MRC,对使用 FMDB 都没有任何影响,FMDB 会在编译项目时自动匹配。 FMDB 将 SQLite API 进行了很友好的封装,使用起来非常方便。
玩转SQLite4:SQLite数据插入与查看 之前两篇文章,介绍了**命令行**和**图形化**的方式进行**数据库的创建**和**表的创建**,相当于创建了一个框架,还没有具体数据,本篇就来介绍如何将数据添加到数据库的表中,以及如何查看表中的数据。 同样,本篇继续使用**命令行**和**图形化**两种方式进行操作。
玩转SQLite3:SQLite图形软件基本操作 上篇文章介绍了sqlite3命令行操作来创建数据库与表,该方法需要有一定的数据库基础知识。 本篇,使用图形化的工具,来实现同样的功能,并且这些方式,不需要熟悉SQL命令。
mysql数据库的常用方法,增删改查 RDBMS DQL:数据查询语言,用于对数据进行查询,如select DML:数据操作语言,对数据进行增加、修改、删除,如insert、udpate、delete TPL:事务处理语言,对事务进行处理,包括begin transaction、commit、rollback DCL:数据控制语言,进行授权与权限回收,如grant、revoke DDL:数据定义语言,进行数据库、表的管理等,如create、drop CCL:指针控制语言,通过控制指针完成表的操作,如declare cursor
SQLite 数据库访问 SQLite是一个很轻量的数据库。详细介绍我就不多说了,这个东西应该在嵌入式里面用得比较多。根据我看到的资料(几个月以前看的),现在安卓应用应该也会用这个,HTML5 也支持sqlite。 SQLite也支持 SQL 语句。
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
ViewController.h:
#import UIKit/UIKit.h @interface ViewController : UIViewController - (IBAction)addClick:(id)sender; - (IBAction)deleteClick:(id)sender; - (IBAction)updateClick:(id)sender; - (IBAction)selectClick:(id)sender; @property (retain, nonatomic) IBOutlet UITextField *idText; @property (retain, nonatomic) IBOutlet UITextField *nameText; - (IBAction)viewClick:(id)sender; @property (retain, nonatomic) IBOutlet UISearchBar *txtSearchBar; @endViewController.m:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * document = [path objectAtIndex:0]; return [document stringByAppendingPathComponent:kFilename]; - (IBAction)addClick:(id)sender { //创建 sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); char * update = "insert or replace into FIELDS (id, name)" "values (?,?);"; sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, update, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 sqlite3_bind_text(stmt, 2, [self.nameText.text UTF8String], -1, NULL);//2表示第二个问号 if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error updating table:%s",errorMsg); sqlite3_finalize(stmt); //这句话不能漏 sqlite3_close(database); //关闭数据库 UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"添加成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)deleteClick:(id)sender { sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg);
sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, del, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error delete table:%s",errorMsg); sqlite3_finalize(stmt); sqlite3_close(database); UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"删除成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)updateClick:(id)sender { sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); char * update = "update FIELDS set id = ?,name = ? where id = ?"; sqlite3_stmt * stmt; if(sqlite3_prepare_v2(database, update, -1, stmt, nil) == SQLITE_OK) sqlite3_bind_int(stmt, 1, [self.idText.text intValue]); //1表示第一个问号 sqlite3_bind_text(stmt, 2, [self.nameText.text UTF8String], -1, NULL);//2表示第二个问号 sqlite3_bind_int(stmt, 3, [self.idText.text intValue]); if(sqlite3_step(stmt)!=SQLITE_DONE) NSAssert(0, @"Error updating table:%s",errorMsg); sqlite3_finalize(stmt); //这句话不能漏 sqlite3_close(database); //关闭数据库 UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"修改成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; - (IBAction)selectClick:(id)sender { //创建数据库 sqlite3 * database; if(sqlite3_open([[self dataFilePath] UTF8String], database)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Failed to open database"); NSString * creataSQL = @"CREATE TABLE IF NOT EXISTS FIELDS" "(id INTEGER PRIMARY KEY,name TEXT);"; char * errorMsg; if(sqlite3_exec(database, [creataSQL UTF8String], NULL, NULL, errorMsg)!=SQLITE_OK) sqlite3_close(database); NSAssert(0, @"Error creating table:%s",errorMsg); NSString * query = @"select * from FIELDS where id = ?"; sqlite3_stmt * statement; if(sqlite3_prepare_v2(database, [query UTF8String], -1, statement, nil) == SQLITE_OK) sqlite3_bind_int(statement, 1, [self.txtSearchBar.text intValue]); //sqlite3_bind_text(statement, 2, [self.nameText.text UTF8String], -1, NULL); while(sqlite3_step(statement) == SQLITE_ROW) int id = sqlite3_column_int(statement, 0); //0表示第1列 char * name = (char *)sqlite3_column_text(statement, 1); //1表示第2列 NSString * sid = [[NSString alloc] initWithFormat:@"%d",id]; NSString * sname = [[NSString alloc] initWithUTF8String:name]; self.nameText.text = sname; self.idText.text = sid; UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"查询成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; sqlite3_finalize(statement); else NSLog(@"error");
SQLite关于数据库和表的操作 SQLite 的 DETACH DATABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的。如果同一个数据库文件已经被附加上多个别名,DETACH 命令将只断开给定名称的连接,而其余的仍然有效。您无法分离 main 或 temp 数据库。 SQLite 的 DETACH DATABASE ‘Alias-Name’ 语句的基本语法如下:
SQLite编程时列出所有在数据库中创建的表 因为所有的点命令只在 SQLite 提示符中可用,所以当您进行带有 SQLite 的编程时,您要使用下面的带有 sqlite_master 表的 SELECT 语句来列出所有在数据库中创建的表:
FMDB | 实现数据的增删改查 FMDB是一个轻量级的数据库,用于将网络资源存储在本地。 项目中使用 ARC 还是 MRC,对使用 FMDB 都没有任何影响,FMDB 会在编译项目时自动匹配。 FMDB 将 SQLite API 进行了很友好的封装,使用起来非常方便。
玩转SQLite4:SQLite数据插入与查看 之前两篇文章,介绍了**命令行**和**图形化**的方式进行**数据库的创建**和**表的创建**,相当于创建了一个框架,还没有具体数据,本篇就来介绍如何将数据添加到数据库的表中,以及如何查看表中的数据。 同样,本篇继续使用**命令行**和**图形化**两种方式进行操作。
玩转SQLite3:SQLite图形软件基本操作 上篇文章介绍了sqlite3命令行操作来创建数据库与表,该方法需要有一定的数据库基础知识。 本篇,使用图形化的工具,来实现同样的功能,并且这些方式,不需要熟悉SQL命令。
mysql数据库的常用方法,增删改查 RDBMS DQL:数据查询语言,用于对数据进行查询,如select DML:数据操作语言,对数据进行增加、修改、删除,如insert、udpate、delete TPL:事务处理语言,对事务进行处理,包括begin transaction、commit、rollback DCL:数据控制语言,进行授权与权限回收,如grant、revoke DDL:数据定义语言,进行数据库、表的管理等,如create、drop CCL:指针控制语言,通过控制指针完成表的操作,如declare cursor
SQLite 数据库访问 SQLite是一个很轻量的数据库。详细介绍我就不多说了,这个东西应该在嵌入式里面用得比较多。根据我看到的资料(几个月以前看的),现在安卓应用应该也会用这个,HTML5 也支持sqlite。 SQLite也支持 SQL 语句。
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
相关文章
- 【DSP开发】解读TI的KeyStone II云技术应用
- Flask中的请求上下文和应用上下文
- 大数据应用还处于早期——专访Hadoop之父Doug Cutting
- Windows8应用开发权威指南
- spectacle 很好用的应用分屏工具Mac
- Linux下安装docker,更改镜像仓库地址,并部署springboot应用
- 浪潮集团王兴山:数字化转型时代 再定义ERP应用价值
- 【Pyhton基础】字典:定义 || 基本使用:增删改查 || 其他操作:统计键值对数量、合并数量、清空数量 || 循环遍历 || 应用场景:和列表组合使用
- 工业智能网关BL110应用之八十一: 实现西门子S7-400 PLC 接入亚马逊云平台
- 增强现实中的光学透射式头盔显示器的应用实例
- java实战应用:MyBatis实现单表的增删改
- Android应用市场转移【2021-03-22】
- 【COM原理和应用】2、COM对象和接口