0

我正在开发一个应用程序,该应用程序从文本字段中获取输入并将其放入字符串中。我有一个表,其中有一个字段,我想根据数据库中字段中的值检查输入中的字符串值。我是 iOS 新手,对 SQLite 还很陌生。

代码:

-(IBAction)setInput:(id)sender
{
NSString *strStoreNumber;
NSString *strRegNumber;

strStoreNumber = StoreNumber.text;
strRegNumber = RegNumber.text;
lblStoreNumber.text = strStoreNumber;
lblRegNumber.text = strRegNumber;

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString* documentsDirectory = [paths lastObject];
//    NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"tblStore.sqlite"];
NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{

    NSLog(@"Opened sqlite database at %@", databasePath);

    //...stuff
} 
else 
{
    NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
    sqlite3_close (database);
}

NSString *querystring;

// create your statement
querystring = [NSString stringWithFormat:@"SELECT strStore FROM tblStore WHERE strStore = %@;", strStoreNumber];  

const char *sql = [querystring UTF8String];

NSString *szStore = nil;
NSString *szReg = nil;


if (sqlite3_prepare_v2(database, sql, -1, &databasePath, NULL)!=SQLITE_OK) //queryString = Statement
{
    NSLog(@"sql problem occured with: %s", sql);
    NSLog(@"%s", sqlite3_errmsg(database));
}
else
{
    // you could handle multiple rows here

    while (sqlite3_step(databasePath) == SQLITE_ROW) // queryString = statement
    {            
        szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(databasePath, 0)];
        szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(databasePath, 1)];
    } // while        

}

sqlite3_finalize(databasePath);

// Do something with data...  
}

它到达“NSLog(@"Opened sqlite database at %@", databasePath);”行,所以它看起来好像可以访问数据库。但是,当我运行该应用程序时,我得到“NSLog(@"sql 问题发生在:%s", sql);” 错误,我可以在控制台中看到。此外,在控制台中,它显示“没有这样的表:tblStore”。

我使用 Firefox 附加组件 SQLite 管理器创建了表。我将 sqlite3 库添加到项目中。我将在 SQLite 管理器中创建的数据库表拖放到我的项目中,在我的两个 AppDelegate 文件和两个 ViewController 文件上方。

任何帮助或输入将不胜感激。谢谢!

编辑:我已将文件正确添加到项目中,看起来好像现在找到了表。不过,现在我有一些奇怪的警告:

“将'const char *'传递给'sqlite3_stmt *'类型参数的不兼容指针类型(又名'struct sqlite3_stmt *')”

此警告出现在以下代码行中:

if (sqlite3_prepare_v2(database, sql, -1, &databasePath, NULL)!=SQLITE_OK)

while (sqlite3_step(sql) == SQLITE_ROW)

szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sql, 0)];

szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(sql, 1)];

sqlite3_finalize(sql);

它与“sql”有关,但我不确定是什么。有什么建议么?

4

3 回答 3

3

Your code seems ok - did you copy the db to the ressource folder of your project?

EDIT

Make sure you access your db file with something like that:

    - (void) initializeDB {

        // Get the database from the application bundle
        NSString* path = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
        {
            NSLog(@"Opening Database"); 
        }
        else
        {
            // Call close to properly clean up
            sqlite3_close(database);

            NSAssert1(0, @"Error: failed to open database: '%s'.", 
                      sqlite3_errmsg(database));
        }

}
于 2012-05-04T17:53:52.613 回答
1

添加到项目中的数据库文件将嵌入到主文件中NSBundle(请参阅 参考资料[NSBundle mainBundle])。

为了做您想做的事,您需要先将数据库从主包复制到文档文件夹,然后再尝试访问它。否则,正如您所遇到的,您将无法在文档文件夹中找到 SQLite DB。

于 2012-05-04T17:56:19.240 回答
0

您可以复制您的数据库,单击查找器并在查找器中写入此地址(/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/)单击确定。你会得到跟单路径。打开您的项目文档文件并粘贴您的数据库....

于 2014-01-20T07:26:45.170 回答