The detailed use of sqlite in iOS
- 2020-06-03 08:30:46
- OfStack
This article shares the specific operation method of sqlite in ios for your reference. The specific content is as follows
#import <sqlite3.h>
@interface ViewController ()
{
sqlite3 *_sqldb;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self OpenDb];
[self createTable];
[self insertData];
[self FindData];
}
// Open the database
-(void)OpenDb{
NSArray *arrs= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Create the database, open it directly if it exists, and open it if it doesn't
NSString *path=[arrs lastObject] ;
NSString *documentpath= [path stringByAppendingPathComponent:@"sql.db"];
int reslut= sqlite3_open([documentpath UTF8String], &_sqldb);
if(reslut==SQLITE_OK){
NSLog(@" The database has been opened ");
}
}
// Create the table from the database instance
-(void)createTable{
// No parameters sql statements
const char* sql="create table if not exists t_person (id integer primary key autoincrement,name text,age integer);";
char *error;
//sqlite3_exec You can perform 1 Cut without parameters SQL Statements. If it is with parameters, it is best not to, prevent SQL Injection vulnerability attack
int resutl= sqlite3_exec(_sqldb, sql, NULL, NULL, &error);
if(resutl==SQLITE_OK){
NSLog(@" Table created successfully ");
}
else{
NSLog(@" Table creation failed -- %s",error);
}
}
// Insert data
-(void)insertData{
// With parameters SQL statements "?" Is a placeholder with arguments
const char * sql="insert into t_person(name,age) values(?,?);";
sqlite3_stmt *stmp;
// In the implementation SQL Check before statement SQL Statement syntax ,-1 Represents the length of the string
int result= sqlite3_prepare_v2(_sqldb, sql, -1, &stmp, NULL);
if(result==SQLITE_OK){
NSLog(@" insert SQL Statement syntax is fine ");
// Binding parameters , The index of the inserted parameter is from 1 start
sqlite3_bind_text(stmp, 1, "gcb", -1, NULL);
sqlite3_bind_int(stmp, 2, 12);
// Execute parameter SQL Statement, can't have exec
int result=sqlite3_step(stmp);
// Insert to determine , Want to use sqLite_Done To judge
if(result==SQLITE_DONE){
NSLog(@" Insert the success ");
}
else{
NSLog(@" Insert the failure ") ;
}
}
else{
NSLog(@" insert SQL Something is wrong with the statement ");
}
}
-(void)FindData{
char *sql="select id,name,age from t_person";
// Query preparation step perform
sqlite3_stmt *stmt;
// check SQL Syntax problems with statements
int result= sqlite3_prepare_v2(_sqldb, sql, -1, &stmt, NULL);
if(result==SQLITE_OK){
while (sqlite3_step(stmt)==SQLITE_ROW) {
// The column of the query is 0 start Insert columns from 1 start
// int xh = sqlite3_column_int(stmt, 0);
int xh=sqlite3_column_int(stmt, 0);
char * name=(char *)sqlite3_column_text(stmt, 1);
int age=sqlite3_column_int(stmt, 2);
NSLog(@"xh=%i-->name=%s-->age=%i",xh,name,age);
}
}
else{
NSLog(@" The query SQL Grammar is wrong ");
}
}
Above is the entire content of this article, I hope to help you with your study.