DBStockManager.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface DBStockManager : NSObject
@property sqlite3 *databaseHandle;
@property NSString *databasePath;
@property NSString *databaseName;
-(void)initDatabase;
-(void)checkAndCreateDatabase;
-(void) insertintoDataBase;
@end
DBStockManger.m
#import "DBStockManager.h"
@implementation DBStockManager
static DBStockManager *_database;
@synthesize databasePath;
@synthesize databaseName;
-(void)initDatabase{
databaseName=@"stock.db";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
databasePath = [documentsDirectory stringByAppendingPathComponent:databaseName];
NSLog(@"%@",databasePath);
[self checkAndCreateDatabase];
}
-(void)checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success=[fileManager fileExistsAtPath:databasePath];
if(success)
return;
NSString *databasePathFromApp=[[[NSBundle mainBundle]resourcePath] stringByAppendingString:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void) insertintoDataBase{
sqlite3 *database;
BOOL returnCode='YES';
NSLog(@"Called insertdatabase");
if(sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK){
NSLog(@"database opened");
char *sqlstatment="insert into user_stock(user_name,user_password) values(?,?)";
sqlite3_stmt *compiledStatment;
if(sqlite3_prepare_v2(database, sqlstatment, -1, &compiledStatment, NULL)==SQLITE_OK){
sqlite3_bind_text(compiledStatment, 1, "nimi", -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatment, 2, "anand", -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(compiledStatment)!=SQLITE_DONE){
NSLog(@"Error:%s",sqlite3_errmsg(database));
}
}
}
- (void)dealloc {
sqlite3_close(_databaseHandle);
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dbManager = [[DBStockManager alloc]init];
[_dbManager initDatabase];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)loginClicked:(id)sender {
_dbManager.insertintoDataBase;
}
@end
我是使用sqlite3的ios app新手。我使用终端创建了stock.db文件。创建了一个表并在表中添加了几条记录。我试图在点击没有发生的按钮时将记录插入数据库。
这是将记录插入表user_Stock的代码。 我总是得到错误错误:没有这样的表:user_stock。但我在数据库中有表。请告知如何继续。
数据库:
Last login: Thu Dec 1 13:00:25 on ttys003
thiyagarajans-MacBook-Pro:~ rajkumar$ ls
Applications Downloads Movies Public stock.sql
Desktop IdeaProjects Music eclipse student.db
Documents Library Pictures stock.db
thiyagarajans-MacBook-Pro:~ rajkumar$ .databases
-bash: .databases: command not found
thiyagarajans-MacBook-Pro:~ rajkumar$ sqlite3
SQLite version 3.14.0 2016-07-26 15:17:14
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main
sqlite> .open stock.db
sqlite> select * from user_Stock;
1|nirmal|anand
2|nimi.hbk|kingkong@123
sqlite>