Objective-cを使用してXcodeにSQLite DBを構築しようとしていますが、テーブルの作成に問題があります。私は挿入機能が正しく実行されたと信じていますが、実行しようとすると次のエラーが表示されます。Objective-CでSQLite DBを作成中にエラーが発生しました
2016-09-20 09:39:31.612テスト[58546:5169036] :ユーザー
私のコードは以下の通りです。私が間違って何をしているのか知っていれば教えてください。ありがとう!
[super viewDidLoad];
//do any addtional setup after view load, typically from nib
NSString *docsDir;
NSArray *dirPaths;
//gets the directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
//Build path to keep database
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Users.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath:_databasePath] == NO) {
const char *dbPath = [_databasePath UTF8String];
if(sqlite3_open(dbPath, &_DB) == SQLITE_OK) {
char *errorMessage;
const char *sqlStatement = "CREATE TABLLE IF NOT EXIST Users (Username TEXT PRIMARY KEY, PASSWORD TEXT, EMAIL TEXT, null PHONE TEXT, null WINS INTEGER, null LOSSES INTEGER, null BALANCE DECIMAL INTEGER";
if(sqlite3_exec(_DB, sqlStatement, NULL, NULL, &errorMessage) != SQLITE_OK) {
//below creates a popup success message if necessary
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Success"
message:@"Table created"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
sqlite3_close(_DB);
}
else {
//below creates a popup error message if necessary
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Error"
message:@"Unable to create table"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
}
}
ご迷惑をおかけして申し訳ありませんが、これを明確にするために私はDBを作成するためのコードです。私はこれが問題のどこにあるのかと考えています。私の挿入を実行しようとすると、 "Users"という表は作成されません。 – dgelinas21
あなたはこれをどこで実行していますか? on Simulator?1。クリーンなビルドを行い、アプリをアンインストールしてもう一度実行してみてください。 2.問題が解決しない場合は、更新する前に選択したクエリを実行して、テーブル名が存在するかどうかを確認します。 –