sqlite数据库的操作

前端之家收集整理的这篇文章主要介绍了sqlite数据库的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

-fno-objc-arc

- (void)viewDidLoad

{

[superviewDidLoad];

//创建数据库保存登录信息

NSString *docsDir;

NSArray *dirPaths;


// Get the documents directory NSString *databasePath;sqlite3 *contactDB;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);


docsDir = [dirPathsobjectAtIndex:0];

// Build the path to the database file

databasePath = [[NSStringalloc] initWithString: [docsDirstringByAppendingPathComponent: @"contacts.db"]];

NSLog(@"表名%@",NSHomeDirectory());

NSFileManager *filemgr = [NSFileManagerdefaultManager];


if ([filemgr fileExistsAtPath:databasePath] ==NO) {

const char *dbpath = [databasePathUTF8String];

if (sqlite3_open(dbpath,&contactDB)==sqlITE_OK) {

char *errMsg;

constchar *sql_stmt ="CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PASSWORD TEXT,STATE BOOL)";

if (sqlite3_exec(contactDB,sql_stmt,NULL,&errMsg)!=sqlITE_OK) {

NSLog(@"创建表失败");

}

else{

sqlite3_stmt *statement;

NSString *insertsql = [NSStringstringWithFormat:@"INSERT INTO CONTACTS (name,password,state) VALUES(\"%@\",\"%@\",\"%d\")",@"",0];

const char *insert_stmt = [insertsqlUTF8String];

sqlite3_prepare_v2(contactDB,insert_stmt,-1,&statement,NULL);

if (sqlite3_step(statement)==sqlITE_DONE) {

NSLog(@"以存储到数据库");

}

sqlite3_finalize(statement);

}

sqlite3_close(contactDB);

}

else{

NSLog(@"创建./打开数据库失败");

}

m_isChecked = FALSE;

_login_name.text =@"";

_login_psw.text =@"";

}

else{

sqlite3_stmt *statement;

const char *dbpath = [databasePathUTF8String];

if (sqlite3_open(dbpath,&contactDB)==sqlITE_OK)

{

//执行查询

NSString *query =@"SELECT NAME,PASSWORD,STATE FROM CONTACTS";

//sqlite3_stmt *statement;

if (sqlite3_prepare_v2(contactDB,[queryUTF8String],nil) == sqlITE_OK) {

//依次读取数据库表格FIELDS中每行的内容,并显示在对应的TextField

if (sqlite3_step(statement) ==sqlITE_ROW) {

//获得数据

m_isChecked = sqlite3_column_int(statement,2);

char *rowNameData = (char *)sqlite3_column_text(statement,0);

char *rowpasswordData = (char *)sqlite3_column_text(statement,1);

//设置文本

_login_name.text = [[NSStringalloc] initWithUTF8String:rowNameData];

_login_psw.text = [[NSStringalloc] initWithUTF8String:rowpasswordData];

}

sqlite3_finalize(statement);

}

else{

NSLog(@"duqu失败");

}

}

sqlite3_close(contactDB);

//

if (!m_isChecked) {

_login_name.text =@"";

_login_psw.text =@"";

}

}


按钮执行的方法

sqlite3_stmt *statement;

const char *dbpath = [databasePathUTF8String];

if (sqlite3_open(dbpath,&contactDB)==sqlITE_OK) {

NSString *insertsql = [NSStringstringWithFormat:@"UPDATE CONTACTS SET name=\"%@\",password=\"%@\",state=%d",_login_name.text,_login_psw.text,m_isChecked];

const char *insert_stmt = [insertsqlUTF8String];

sqlite3_prepare_v2(contactDB,NULL);

if (sqlite3_step(statement)==sqlITE_DONE) {

NSLog(@"以存储到数据库");

// _login_name.text = @"";

// _login_psw.text =@"";

NSLog(@"%@",_login_name.text);

NSString *userName = _login_name.text;

NSString *userPassword = _login_psw.text;

[[NSUserDefaultsstandardUserDefaults] setValue:userNameforKey:@"userName"];

[[NSUserDefaultsstandardUserDefaults] setValue:userPassword forKey:@"userPassword"];

}

else{

NSLog(@"保存失败");

}

sqlite3_finalize(statement);

sqlite3_close(contactDB);

}

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

猜你在找的Sqlite相关文章