如何在sqlite中删除数据 [英] how delete data in sqlite

查看:407
本文介绍了如何在sqlite中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#import" DBManager.h"

静态DBManager * sharedInstance = nil;

static sqlite3 * database = nil;

static sqlite3_stmt * statement = nil;



@implementation DBManager

+(DBManager *)getSharedInstance {

if( !sharedInstance){

sharedInstance = [[super allocWithZone:NULL] init];

[sharedInstance createDB];

}

返回sharedInstance;

}



- (BOOL)createDB {

NSString * docsDir;

NSArray * dirPaths;

//获取文档目录

dirPaths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory,NSUserDomainMask,是);

docsDir = dirPaths [0];

//构建数据库文件的路径

databasePath = [[NSString alloc] initWithString:

[docsDir stringByAppendingPathComponent:@&q uot; student.db"]];

BOOL isSuccess = YES;

NSFileManager * filemgr = [NSFileManager defaultManager];

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

{

const char * dbpath = [databasePath UTF8String];

if(sqlite3_open(dbpath, &数据库)== SQLITE_OK)

{

char * errMsg;

const char * sql_stmt ="如果不存在则创建表studentDetail (regno integer primary key,name text,department text,year text)" ;;

if(sqlite3_exec(database,sql_stmt,NULL,NULL,& errMsg)

!= SQLITE_OK)

{

isSuccess = NO;

NSLog(@无法创建表格);

}

sqlite3_close(数据库);

返回isSuccess;

}

否则{

isSuccess = NO;

NSLog(@无法打开/创建数据库);

}

}

返回isSuccess;

}





创建sqlite db成功并且还在其中添加了数据......

如何删除sqlitedb中的数据,其中regno为id ....

#import "DBManager.h"
static DBManager *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;

@implementation DBManager
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}

-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"student.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt ="create table if not exists studentsDetail (regno integer primary key, name text, department text, year text)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}


created sqlite db successfully and also added data in it ...
how to delete data in sqlitedb with regno as id....

推荐答案

尝试:
DELETE FROM MyTable WHERE regno=1





参见: http://www.sqlite.org/lang_delete.html [ ^ ]


这篇关于如何在sqlite中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆