zl程序教程

您现在的位置是:首页 >  工具

当前栏目

sqlite3增删查改应用

应用 增删 sqlite3 查改
2023-09-27 14:29:23 时间
    sqlite3数据库可以采用MesaSQLite可视化工具 2.添加sqlite3支持的库文件,libsqlite3.dylib 3.创建viewController控制器,布局好界面
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;

@end
ViewController.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!