English 中文(简体)
SQLite3(iOS) 查询未执行
原标题:SQLite3 (iOS) query not executing

我有点麻烦 使这个工作。

这是我要执行的代码 :

[self openDB];
sqlite3_stmt *statement;
NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, Percentage, UserId) VALUES ( ola232332332324 ,  2012-02-03 , 1, 1, NULL, 1)"];
if(sqlite3_prepare_v2(db, [sql2 UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
    alert1 = [[UIAlertView alloc]
              initWithTitle:@"Grats" message:@"The query was executed!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles: nil];
    [alert1 show];
    [alert1 release];
} 
else {
    NSAssert(0, @"Failed to execute");
}
sqlite3_finalize(statement);

这是我用来调用DB的代码:

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    return [documentsDir stringByAppendingPathComponent:@"prosegur.sqlite"];
}

-(void) openDB {
    sqlite3_open([[self filePath] UTF8String], &db);
    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
        NSAssert(0, @"Failed to open db");
    }
}

Edit: 试图使用调频数据库,这里是代码 :

FMDatabase *database = [FMDatabase databaseWithPath:@"prosegur.sqlite"];
if([database open]){
    NSLog(@"yes");
    [database executeUpdate:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, UserId) VALUES ( cenas ,  2012-02-0 ,  1, 1, 1)"];}
else
{
    NSLog(@"not");
}

通过 [数据库打开] 校验, 但它不执行查询 。

EDIT2: 已经做了建议,我开始认为它建立了一个数据库,在数据库内执行查询,但由于它不存在任何表格,它忽略了这一事实。

最新代码如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [[paths objectAtIndex:0] retain];
NSString *path = [[docsPath stringByAppendingPathComponent:@"prosegur.sqlite"] retain];

FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
if([database open])
{
    NSLog(@"yes");
    [database executeUpdate:@"INSERT INTO CheckList (CLName) VALUES (?)", @"cenas"];
}
else 
{
    NSLog(@"no");
}
[database close]; 
最佳回答

I found out the answer, if anyone is having troubles like this, check out the following link: IPhone Development - Failed to use SQLite

感谢大家的支持!

问题回答

可能是一个愚蠢的问题, 但您知道 sqlite3_ prepare_v2 仅仅是 prepares 声明, 而实际上不是 execut 声明? 您也需要拨打 sqlite3_ step 来实际插入,

您可以使用 sqlite3_exec 来代替 sqlite3_exec ,它只执行一个声明而不准备/执行两步。

I have had a lot of headache while using standart sqlite3 api in iOS development. Check out FMDB framework. It is very simple to use, and you can find lots of tutorials on the web.

如果您不想要这个框架, 请尝试使用 < code> NSError 来抓取查询执行中的错误 。

示例。

+(BOOL)InsertDataInPin:(NSString *)pin
    {
        NSString *databasePath =Your DatabasePath;

        NSString *SQL=@"INSERT INTO pincode Values(?)";
        sqlite3_stmt *dataset;


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

            if (sqlite3_prepare_v2(database, [SQL UTF8String], -1, &dataset, NULL) == SQLITE_OK) 
            {

                sqlite3_bind_text(dataset, 1, [pin UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_step(dataset);

            }       

            sqlite3_close(database);
        }


    }




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签