gpt4 book ai didi

ios Sqlite 连接

转载 作者:行者123 更新时间:2023-11-29 02:18:12 25 4
gpt4 key购买 nike

我的问题是我正在尝试将我的数据库连接到 sqlite。它总是出现错误“找不到表”..我现在该怎么办..请帮忙这是我的 opendatabaseconnectivity 代码....

BOOL openDatabaseResult = sqlite3_open([databasePath UTF8String], &sqlite3Database);
if(openDatabaseResult == SQLITE_OK) {
// Declare a sqlite3_stmt object in which will be stored the query after having been compiled into a SQLite statement.
sqlite3_stmt *compiledStatement;

// Load all data from database to memory.
BOOL prepareStatementResult = sqlite3_prepare_v2(sqlite3Database, query, -1, &compiledStatement, NULL);
if(prepareStatementResult == SQLITE_OK) {
// Check if the query is non-executable.
if (!queryExecutable){
// In this case data must be loaded from the database.

// Declare an array to keep the data for each fetched row.
NSMutableArray *arrDataRow;

// Loop through the results and add them to the results array row by row.
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Initialize the mutable array that will contain the data of a fetched row.
arrDataRow = [[NSMutableArray alloc] init];

// Get the total number of columns.
int totalColumns = sqlite3_column_count(compiledStatement);

// Go through all columns and fetch each column data.
for (int i=0; i<totalColumns; i++){
// Convert the column data to text (characters).
char *dbDataAsChars = (char *)sqlite3_column_text(compiledStatement, i);

// If there are contents in the currenct column (field) then add them to the current row array.
if (dbDataAsChars != NULL) {
// Convert the characters to string.
[arrDataRow addObject:[NSString stringWithUTF8String:dbDataAsChars]];
}

// Keep the current column name.
if (self.arrColumnNames.count != totalColumns) {
dbDataAsChars = (char *)sqlite3_column_name(compiledStatement, i);
[self.arrColumnNames addObject:[NSString stringWithUTF8String:dbDataAsChars]];
}
}

// Store each fetched data row in the results array, but first check if there is actually data.
if (arrDataRow.count > 0) {
[self.arrResults addObject:arrDataRow];
}
}
}
else {
// This is the case of an executable query (insert, update, ...).

// Execute the query.
BOOL executeQueryResults = sqlite3_step(compiledStatement);
if (executeQueryResults == true) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);

// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
}
else {
// If could not execute the query show the error message on the debugger.
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
}
}
else {
// In the database cannot be opened then show the error message on the debugger.
NSLog(@"%s", sqlite3_errmsg(sqlite3Database));
}

// Release the compiled statement from memory.
sqlite3_finalize(compiledStatement);

}

// Close the database.
sqlite3_close(sqlite3Database);
}

最佳答案

在访问你的数据库之前,你是否将它复制到文档目录中?

像这样:

static sqlite3 *database = nil;

+ (void)openDatabase
{
[self copyDatabaseIfNeeded];
(sqlite3_open([[self getDBPath] UTF8String],&database));
}

+ (NSString *)getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSLog(@"%@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"databaseName.sqlite"];
}

+ (void)copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];

NSLog(@"dbpath=%@",dbPath);
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databaseName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

if (!success)
NSAssert1(0, @"Failed to create writable database file with message \"%@\".", [error localizedDescription]);
}
}

在使用前还要检查您要访问的表是否已在数据库中创建。

关于ios Sqlite 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com