如何从sqlite管理器中检索数据

时间:2014-11-07 08:30:33

标签: ios iphone ios7 ios5 ios6

我创建了数据库并使用sqlite manager(mozilla)创建了表。现在我想将这些数据检索到我的iOS应用程序中。如何以编程方式进行操作。你能不能请任何人帮我怎么做。谢谢。

3 个答案:

答案 0 :(得分:1)

使用类似的东西:

导入:

#import "sqlite3.h"

检查数据库是否存在:

NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;
sqlite3 *DB;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"YourDbName.sqlite"]]; //put your db name here

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &DB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS YourTable (Value INTEGER PRIMARY KEY, column TEXT)";


        if (sqlite3_exec(DB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {

        }

        sqlite3_close(DB);

    }
}

获取数据库值:

    const char *dbpath = [databasePath UTF8String];
        sqlite3_stmt    *statement;


        if (sqlite3_open(dbpath, &DB) == SQLITE_OK)  //News is a sqlite variable initialized like this: sqlite3* News;
        {
            NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM YourDBName"];

            const char *query_stmt = [querySQL UTF8String];

            if (sqlite3_prepare_v2(DB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
            {
                while(sqlite3_step(statement) == SQLITE_ROW)
                {
                    NSString* example; //example variable to assign data from db

                    example = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; //change the 0 to 1,2,3.... for every column of your db

                }


                sqlite3_finalize(statement);
            }
            sqlite3_close(DB);
        }

答案 1 :(得分:-1)

要将该数据检索到iOS应用程序中,请按照以下步骤操作 让我们考虑您的SQlite数据库具有名称YourDB,其中包含表信息和列Name,Place和City以及DatabasePath是存储SQlite DB的路径

 const char *dbpath = [DatabasePath UTF8String];
sqlite3_stmt    *statement;

if (sqlite3_open(dbpath, &YourDB) == SQLITE_OK)
{
 NSString *querySQL = [NSString stringWithFormat:
                          @"SELECT Place, City FROM students WHERE Name=Bob"];
 const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(YourDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
           NSString *Place = [[NSString alloc]
                                      initWithUTF8String:
                                      (const char *) sqlite3_column_text(
                                                                         statement, 0)];

            NSString *City = [[NSString alloc]
                                    initWithUTF8String:(const char *)
                                    sqlite3_column_text(statement, 1)];
   }
       sqlite3_finalize(statement);
    }
    sqlite3_close(YourDB);
}

答案 2 :(得分:-2)

将“sqlite”文件放入App的捆绑包中,然后就可以使用该文件了。