這是用於數據庫
-(void)updateTable:(NSString *)tableName setname:(NSString *)Name setImagePath:(NSString *)imagePath whereID:(NSInteger)rid{
NSString *sqlString=[NSString stringWithFormat:@"update %@ set name='%@' where id=%ld",tableName,Name,rid];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(@"Faield to update");
}
else{
NSLog(@"update successfully");
}
}
-(void)deleteFrom:(NSString *)tablename whereName:(NSInteger)rid {
NSString *sqlString=[NSString stringWithFormat:@"delete from %@ where id=%ld",tablename,(long)rid];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(@"faield to Delete");
}
else{
NSLog(@"Deleted successfully");
}
}
-(void)insertInTable:(NSString *)tableName withName:(NSString *)name withImagePath:(NSString *)imagePath
{
NSString *sqlString=[NSString stringWithFormat:@"insert into %@(name,path)values('%@','%@')",tableName,name,imagePath];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(@"Failed to insert");
}
else{
NSLog(@"Inserted succesfully");
}
}
-(NSString *)path
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
return [documentDir stringByAppendingPathComponent:@"Storage.db"];
}
-(void)open{
if(sqlite3_open([[self path] UTF8String], &db)!=SQLITE_OK)
{
sqlite3_close(db);
NSLog(@"your database table has been crash");
}
else{
NSLog(@"Database open successfully");
}
}
-(void)closeDatabase
{
sqlite3_close(db);
NSLog(@"Database closed");
}
-(void)copyFileToDocumentPath:(NSString *)fileName withExtension:(NSString *)ext{
NSString *filePath=[self path];
NSFileManager *fileManager=[NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
NSString *pathToFileInBundle=[[NSBundle mainBundle] pathForResource:fileName ofType:ext];
NSError *err=nil;
BOOL suc=[fileManager copyItemAtPath:pathToFileInBundle toPath:filePath error:&err];
if (suc) {
NSLog(@"file copied successfully");
}
else
{
NSLog(@"faield to copied");
}
}
else
{
NSLog(@"File allready present");
}
}
-(NSMutableArray *)AllRowFromTableName:(NSString *)tableName{
NSMutableArray *array=[[NSMutableArray alloc] init];
NSString *sqlString=[NSString stringWithFormat:@"select *from %@",tableName];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
Database *tempDatabase=[[Database alloc] init];
tempDatabase.Hid=sqlite3_column_int(statement, 0);
tempDatabase.Hname =[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
tempDatabase.Hpath=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
[array addObject:tempDatabase];
}
}
return array;
}
-(void)test
{
//Pet photos
NSString *sqlString=[NSString stringWithFormat:@"create table if not exists StorageTable(id integer primary key autoincrement,name text,path text)"];
char *error;
if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
[self closeDatabase];
NSLog(@"Faield to blanck 1 %s",error);
}
else{
NSLog(@"Test On StorageTable Database successfully");
}
}
呃,有什麼返回代碼*爲* sqlite3_open返回常見的方法是什麼? sqlite3_errmsg報告什麼? –
(順便說一句,假設你真的在「調試」以找出它失敗的地方,編譯器不參與。) –
我認爲你必須改變文件擴展名,即student.sqlite。只需嘗試 – Kalpesh