sqlite数据库的 *** 作

sqlite数据库的 *** 作,第1张

概述-fno-objc-arc - (void)viewDidLoad {     [superviewDidLoad];      //创建数据库保存登录信息    NSString *docsDir;    NSArray *dirPaths;     // Get the documents directory NSString *databasePath; sqlite3 *contactDB

-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);

}

总结

以上是内存溢出为你收集整理的sqlite数据库的 *** 作全部内容,希望文章能够帮你解决sqlite数据库的 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1172232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存