English 中文(简体)
板块错误数据库被锁定
原标题:sqlite error database is locked

我在我写的“Pi”应用程序上有一个精彩的数据库。 我的手法有了一个错误,我正在经历背景。 在背景阅读中,我称这一方法:

 - (BOOL) songIsInDatabase:(NSString *)songTitle
{
NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];


//Build the path to the database file
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Database.db"]];

const char *dbpath = [databasePath UTF8String];

sqlite3_stmt *statement;

if(sqlite3_open(dbpath, &DB) == SQLITE_OK){

    NSString *insertSQL = [NSString stringWithFormat:@"select * from Bpm_Table where song_title =  %@ ", songTitle];

    const char *insert_stmt = [insertSQL UTF8String];
    if(sqlite3_prepare_v2(DB, insert_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            return YES;
            break;
        }
    }else{
        NSLog(@"the error is %s", sqlite3_errmsg(DB));
    }
    sqlite3_finalize(statement); 
}

[databasePath release];
return NO;
}

然后,我称之为这种方法:

- (void) addSongToDatabase: (NSString *) songTitle andBPM: (int)bpm andGenre: (NSString *) genre
{
NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];


//Build the path to the database file
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Database.db"]];

const char *dbpath = [databasePath UTF8String];

sqlite3_stmt *statement;

if(sqlite3_open(dbpath, &DB) == SQLITE_OK){

    NSString *insertSQL = [NSString stringWithFormat:@"insert or replace into Bpm_Table (song_title, bpm, genre) values ("%@", "%d", "%@")", songTitle, bpm, genre];

    const char *insert_stmt = [insertSQL UTF8String];
    if(sqlite3_prepare(DB, insert_stmt, -1, &statement, NULL) == SQLITE_OK){
        if(sqlite3_step(statement) == SQLITE_DONE)
        {

        } else {
            NSLog(@"error: %s", sqlite3_errmsg(DB));
        }
    }sqlite3_finalize(statement);
}

[databasePath release];
}

如果我采用这两种方法,一种是另一种方法,那么我就有一个错误,即数据库被锁定。 我添加了<条码>sqlite3_finalize的声明,然后在 go角周围 p,希望能确定问题。 如果我谈其中两种方法,我就没有发现这一错误。

谁知道什么错误?

最佳回答

您在使用该数据库后,必须始终关闭该数据库,从而添加以下条目:sqlite3_close(DB);

更新

你在座右边之一回来,

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            return YES;
            break;
        }

因此,你正在对数据库进行最后审定或关闭。 如果你每次开张,你必须关闭。

问题回答

但指出,如果你从多个侧面进入同一个SQ-DB,而没有某种分离的同步(某种方式知道你永远不会有近似的双向通道),那么即使你在每一次访问中都正确关闭了一切,你也有可能发现“数据库锁定”错误。

食堂没有提供任何形式的“24小时等待”机制。

你的基本选择是:

  1. Do all accesses from one thread.
  2. Use a separate lock protocol around all accesses.
  3. If you get a "database locked" error wait a brief period of time and retry.

your database is open close it using sqlite3_close(db) if you dont close then the process which accessed your database will be running background which will cause database is locked error.

if you want to remove database is locked error then follow these steps 1.copy your database file to some other location. 2.then replace the database with the copied database 3.this will dereference all processes which were accessing your database file

If you have tried sqlite3_close(DB) it s probably because your methods are trying to access the same database at the same time. Try this line of code

sqlite3_busy_timeout(DB, 500);

<代码>sqlite3_open和sqlite3_prepare_v2在从哪里获得“数据库被锁定”-ror。

然后,数据库连接将试图在500毫米秒钟内进行书写/阅读,然后才能开台,这通常为躲避锁的足够时间。

你的数据库已经开放。 接近sqlite3_close(db)

如果你不关闭,那么进入你的数据库的过程将具有造成数据库被锁定错误的背景。

For one thing, you re not closing the database connection with sqlite3_close() before you open a new connection

我只花了一小时的时间来处理这个问题。 当我不知觉地通过关闭弹壳(当该笔文字在行程和时间时)而杀死一个过程时,我便会遇到问题,而日报《时报》仍然开放。 视窗即使你在沙尔杀死了它,但不管你使用 d( d())还是用文字定稿,都不会杀死这一进程;如果你撤出所有灰色 app,工作队经理仍然有一人。

因此,您将需要结束Windows 任务主管的所有灰色过程,重新启动这些过程,并且应当予以罚款(if<>,这实际上是问题的根源)。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...