我有一个应用程序,我想要它创建一个表并插入几个值才开始。
我创建数据库表的代码:
- (void) crearTablaConf {
sqlite3 *turutaDB;
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *databasePath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"turuta.db"];
NSLog(@"path de la bbdd: %@", databasePath);
if(sqlite3_open([databasePath UTF8String], &turutaDB)==SQLITE_OK) {
NSLog(@"2 Base de datos creada y abierta con exito");
} else {
NSLog(@"Ha fallado la apertura de la bbdd");
}
sqlite3_stmt *sentenciaTablaConf;
NSString *queryTablaConf = @"CREATE TABLE IF NOT EXISTS configuraciones (id int(11) NOT NULL, idioma int(1),fecha date,PRIMARY KEY (id));";
if(sqlite3_prepare_v2(turutaDB, [queryTablaConf UTF8String], -1, &sentenciaTablaConf, NULL)==SQLITE_OK){
NSLog(@"Consulta preparada ok"); //NO ERROR HERE
} else {
NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB));
}
sqlite3_finalize(sentenciaTablaConf);
sqlite3_stmt *sentenciaIniciar;
NSString *queryIniciar = @"insert into configuraciones (id, idioma, fecha) values ('1','0',NULL);";
if(sqlite3_prepare_v2(turutaDB, [queryIniciar UTF8String], -1, &sentenciaIniciar, NULL)==SQLITE_OK){
NSLog(@"Consulta preparada ok");
} else {
NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB));
} //HERE, SAYS "NO SUCH TABLE: CONFIGURACIONES"
sqlite3_finalize(sentenciaIniciar);
}
我检查了自动提交,然后打开,所以我找不到错误的位置。
答案 0 :(得分:2)
尝试此代码
NSString *docsDir;
NSArray *dirPaths;
sqlite3 *contactDB;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath =[docsDir stringByAppendingPathComponent:@"yourdatabaseName.sqlite"];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, LOCATION TEXT, CONDITION TEXT,HUMIDITY TEXT, WINDCONDI TEXT,PRESSURE TEXT, DEW TEXT, TEMP TEXT,LASTUP TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
else{
//Do anything here
}
sqlite3_close(contactDB);
} else {
NSLog(@"Failed to open/create database");
}
}
else
{
// Do anything here
}
答案 1 :(得分:0)
NSString *queryTablaConf = @"CREATE TABLE IF NOT EXISTS configuraciones (id int(11) NOT NULL, idioma int(1),fecha date,PRIMARY KEY (id));";
在此行之后的代码中添加了此块以成功创建表并检查一次。
if (sqlite3_exec(turutaDB, queryTablaConf, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
答案 2 :(得分:0)
- (void) checkAndCreateDatabase
{
NSString *databaseName = @"CPPPrograms.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSLog(@"Dir : %@ ",documentsDir);
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager] ;
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success)
return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
NSLog(@"%@",databasePathFromApp);
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
//[fileManager release];
}
- (void)getData
{
NSString *databasePath;
sqlite3 *database;
NSString *databaseName;
NSString *documentsDir;
NSArray *documentPaths;
databaseName = @"CPPPrograms.sqlite";
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
NSLog(@"%@",documentsDir);
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "SELECT * FROM CppPrograms";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
[PgmName removeAllObjects];
[PgmHtml removeAllObjects];
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
[PgmName addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 0)]];
[PgmHtml addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(compiledStatement, 1)]];
NSLog(@"%@",[PgmName description]);
NSLog(@"%@",[PgmHtml description]);
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
尝试使用此代码,因为它只是更改表名和字段名,无论您的表名是什么....
答案 3 :(得分:0)
您错过了sqlite3_step()
声明。
实施您的方法,如:
- (void) crearTablaConf
{
sqlite3 *turutaDB;
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *databasePath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"turuta.db"];
if(sqlite3_open([databasePath UTF8String], &turutaDB)==SQLITE_OK)
{
sqlite3_stmt *sentenciaTablaConf;
NSString *queryTablaConf = @"CREATE TABLE IF NOT EXISTS configuraciones (id int(11) NOT NULL, idioma int(1),fecha date,PRIMARY KEY (id));";
if(sqlite3_prepare_v2(turutaDB, [queryTablaConf UTF8String], -1, &sentenciaTablaConf, NULL)==SQLITE_OK)
{
sqlite3_step(sentenciaTablaConf);
sqlite3_finalize(sentenciaTablaConf);
}
else
{
NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB));
}
//Other stuffs
sqlite3_close(turutaDBa);
}
else
{
NSLog(@"Ha fallado la apertura de la bbdd");
}
}