关于SQLite的基本使用

前端之家收集整理的这篇文章主要介绍了关于SQLite的基本使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//sqlite的基本使用-Prefix.pch

1.

//

// Prefix header

// The contents of this file are implicitly included at the beginning of every source file.

//


#ifdef __OBJC__

#import <Foundation/Foundation.h>

#endif


2.

// Prefix header

//

// The contents of this file are implicitly included at the beginning of every source file.

//


#import <Availability.h>


#ifndef __IPHONE_5_0

#warning "This project uses features only available in iOS SDK 5.0 and later."

#endif

#ifdef __OBJC__

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

#endif


#import <Foundation/Foundation.h>


int main(int argc,const char * argv[])

{


@autoreleasepool {

NSMutableString *sql = [NSMutableString string];

NSArray *names = @[@"jack", @"rose",@"jim", @"jake"];

for (int i = 0; i<100; i++) {

int index = arc4random()%names.count;

NSString *namePre = names[index];

NSString *name = [NSString stringWithFormat:@"%@-%d",namePre,arc4random()%100];

int age = arc4random() % 100;

double score = arc4random() % 100;

[sql appendFormat:@"insert into t_student (name,age,score) values('%@',%d,%f);\n",name,score];

}

///Users/mc/Desktop/素材images

///Users/aplle/Desktop/student.sql

[sql writeToFile:@"Users/mc/Desktop/student.sql" atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

return 0;

}


*****************************

#import <Foundation/Foundation.h>


int main(int argc,const char * argv[])

{


@autoreleasepool {

NSArray *names = @[@"西门抽血",@"西门抽筋",@"西门抽风",@"西门吹雪"];

NSMutableString *sql = [NSMutableString string];

for (int i = 0; i<200; i++) {

int ID = i + 1;

int age = arc4random_uniform(20) + 20;

NSString *name = names[arc4random_uniform(names.count)];

name = [name stringByAppendingFormat:@"-%d",arc4random_uniform(100)];

[sql appendFormat:@"insert into t_student (id,age) values (%d,'%@',%d);\n",ID,age];

}

[sql writeToFile:@"/Users/apple/Desktop/students.sql" atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSLog(@"\n%@",sql);

}

return 0;

}





插入数据:更改删除查询--->>>>>>>>>

#import "IWViewController.h"

#import<sqlite3.h>


@interfaceIWViewController()

{

sqlite3*_db;// db代表着整个数据库db数据库实例

}

- (IBAction)insert;

- (IBAction)update;

- (IBAction)delete;

- (IBAction)query;

@end


@implementationIWViewController


- (void)viewDidLoad

{

[superviewDidLoad];

// 0.获得沙盒中的数据库文件

NSString*filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMaskYES)lastObject]stringByAppendingPathComponent:@"student.sqlite" // 1.创建(打开)数据库(如果数据库文件不存在,会自动创建)

intresult =sqlite3_open(filename.UTF8String,&_db);

if(result ==sqlITE_OK) {

NSLog(@"成功打开数据库");

// 2.创表

constchar*sql ="create table if not exists t_student (id integer primary key autoincrement,name text,age integer);";

char*errorMesg =NULL;

sqlite3_exec(_db,sql,NULL,&errorMesg);

sqlITE_OK) {

NSLog(@"成功创建t_student");

}else{

创建t_student表失败:%s",errorMesg);

}

}else{

打开数据库失败");

}

}


- (IBAction)insert

{

for(inti =0; i<30; i++) {

NSString*name = [NSStringstringWithFormat:@"Jack-%d",arc4random()%100];

intage =100;

*sql = [stringWithFormat:@"insert into t_student (name,age) values('%@',%d);"

sql.sqlITE_OK) {

成功添加数据");

}添加数据失败 }

}

}


- (IBAction)update

{


}


- (IBAction)delete

{

}


- (IBAction)query

{

// sql注入漏洞

/**

登录功能

1.用户输入账号和密码

*账号:123' or 1 = 1 or '' = '

*密码:456654679

2.拿到用户输入的账号和密码去数据库查询查询有没有这个用户名和密码)

select * from t_user where username = '123' and password = '456';

*/

// 1.定义sql语句

*sql ="select id,age from t_student where name = ?;";

// 2.定义一个stmt存放结果集

sqlite3_stmt*stmt =NULL;

// 3.检测sql语句的合法性

sqlite3_prepare_v2(1,&stmt,162)">NULL);

查询语句是合法的");

//设置占位符的内容

sqlite3_bind_text(stmt,"jack",162)">NULL);

// 4.执行sql语句,从结果集中取出数据

// int stepResult = sqlite3_step(stmt);

while(sqlite3_step(stmt) ==sqlITE_ROW) {//真的查询到一行数据

获得这行对应的数据

获得第0列的id

intsid =sqlite3_column_int(stmt,216)">0);

//获得第1列的name

constunsignedchar*sname =sqlite3_column_text(stmt,216)">1);

2age

intsage =2);

@"%d %s %d",sid,sname,sage);

}

}查询语句非合法");

}

}

@end


2.*************************

#import "ViewController.h"

#import <sqlite3.h>


@interface ViewController ()

- (IBAction)insert;

- (IBAction)update;

- (IBAction)delete;

- (IBAction)select;

// db就是数据库的象征,如果要进行CRUD,得操作db这个实例

@property (nonatomic,assign) sqlite3 *db;

@end


@implementation HMViewController


- (void)viewDidLoad

{

[super viewDidLoad];

// 获得数据库文件的路径

NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];

NSString *filename = [doc stringByAppendingPathComponent:@"students.sqlite"];

// OC字符串 转成 C语言字符串

const char *cfilename = filename.UTF8String;

// 1.打开数据库(如果数据库文件不存在,sqlite3_open函数自动创建数据库文件

int result = sqlite3_open(cfilename,&_db);

if (result == sqlITE_OK) { // 打开成功

NSLog(@"成功打开数据库");

// 2. const char *sql = "CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL);";

char *erroMsg = NULL;

result = sqlite3_exec(self.db,NULL,&erroMsg);

if (result == sqlITE_OK) {

NSLog(@"成功创表");

} else {

// printf("创表失败--%s--%s-%d",erroMsg,__FILE__,__LINE__);

NSLog(@"创表失败--%s--%@-%d",[NSString stringWithUTF8String:__FILE__],__LINE__);

}

} else {

NSLog(@"打开数据库失败");

}

}


- (IBAction)insert {

for (int i = 0; i<20; i++) {

// 1.拼接sql语句

NSString *name = [NSString stringWithFormat:@"Jack-%d",arc4random_uniform(100)];

int age = arc4random_uniform(20) + 30;

NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_student (name,age) VALUES ('%@',%d);",age];

// 2.执行sql语句

char *erroMsg = NULL;

sqlite3_exec(self.db,sql.UTF8String,&erroMsg);

if (erroMsg) {

NSLog(@"插入数据失败--%s",erroMsg);

} else {

NSLog(@"成功插入数据");

}

}

}


- (IBAction)update {

}


- (IBAction)delete {

}


- (IBAction)select {

const char *sql = "SELECT id,age FROM t_student WHERE age <= 30;";

// 进行查询前的准备工作

// -1 代表系统会自动计算sql语句的长度

// sqlite3_stmt:用来取数据

sqlite3_stmt *stmt = NULL;

if (sqlite3_prepare_v2(self.db,-1,NULL) == sqlITE_OK) { // sql语句没有问题

NSLog(@"查询语句没有问题");

// 每调一次sqlite3_step函数stmt就会指向下一条记录

while (sqlite3_step(stmt) == sqlITE_ROW) { // 找到一条记录

// 取出数据

// 取出第0列字段的值(int类型的值)

int ID = sqlite3_column_int(stmt,0);

// 取出第1列字段的值(tex类型的值)

const unsigned char *name = sqlite3_column_text(stmt,1);

// 取出第2列字段的值(int类型的值)

int age = sqlite3_column_int(stmt,2);

NSLog(@"%d %s %d",age);

}

} else {

NSLog(@"查询语句有问题");

}

}

@end

原文链接:https://www.f2er.com/sqlite/199442.html

猜你在找的Sqlite相关文章