iOS中SQLite的操作方法
今天終于花了點時間把之前項目中使用到的對SQLite操作的方法整理一下并上傳到github上,下載地址:(https://github.com/peanutNote/QYSQLiteManagerDemo.git)。
與其他第三方目的一樣,主要是為了使代碼中有關對SQLite操作簡單化,具體用法:
將QYSQLiteManager文件add到項目中,并在需要對SQLite進行操作的類中添加#import "QYSQLiteManager" 。
// 插入語句
- (void)insertTable
{
// 創(chuàng)建sql語句
NSString *sql = @"insert into teacher(name,id) values(?,?)";
// 不可變參數(shù)
// BOOL isOK = [QYSQLiteManager insertTableWithSqlString:sql andArray:@[@"小明",@115]];
// 可變參數(shù)
BOOL isOK = [QYSQLiteManager insertTableWithSqlString:sql andObjects:@"小明",@"115", nil];
if (isOK) {
NSLog(@"數(shù)據(jù)插入成功");
} else {
NSLog(@"數(shù)據(jù)插入失敗");
}
}
// 查詢語句
- (void)selectTable
{
NSString *sql = @"select * from teacher";
[QYSQLiteManager selectTableWithSqlString:sql didFinishedBlock:^(NSArray *dataList, NSString *error) {
NSLog(@"%@",dataList);
} andObjects:nil];
}
// 修改表語句
- (void)alterTable
{
NSString *sql = @"alter table teacher add column pwd integer";
if([QYSQLiteManager alterTableWithSqlString:sql])
{
NSLog(@"修改成功");
}
}
// 更新數(shù)據(jù)語句
- (void)updateTable
{
NSString *sql = @"update teacher set name = ? where id = ?";
if ([QYSQLiteManager updateTableWithSqlString:sql andArray:@[@"小明",@115]]) {
NSLog(@"更新成功");
}
}
有關查詢語句返回的數(shù)據(jù)類型,有需要的同學可以自行在“QYSQLiteManager.m”中查找“sqlite3_bind_text”,然后在如下處
for (int i = 0; i < column_count; i++) {
// 獲取字段名
char * keyName = (char *)sqlite3_column_name(stmt, i);
NSString *key = [NSString stringWithUTF8String:keyName];
if (sqlite3_column_type(stmt, i) == SQLITE_TEXT) { // 當字段數(shù)據(jù)是“text”時
// 獲取字段對應的數(shù)據(jù)
char *valueName = (char *)sqlite3_column_text(stmt, i);
NSString *value = [NSString stringWithUTF8String:valueName];
[dataDic setObject:value forKey:key];
} else { // 當字段數(shù)據(jù)是integer時
int value = sqlite3_column_int(stmt, i);
[dataDic setObject:@(value) forKey:key];
}
}
修改你自己想要的數(shù)據(jù)類型即可。
以上內容是小編給大家日常收集整理的iOS sqlite對數(shù)據(jù)庫的各種操作,希望對大家有所幫助
相關文章
詳解使用Xcode7的Instruments檢測解決iOS內存泄露(最新)
本篇文章主要介紹使用Xcode7的Instruments檢測解決iOS內存泄露(最新)的相關資料,需要的朋友可以參考下2017-09-09
Drawer?Builder組件實現(xiàn)flutter側邊抽屜效果示例分析
這篇文章主要為大家介紹了Drawer?Builder組件實現(xiàn)flutter側邊抽屜效果示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

