Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Sqlite3/PDO write lock issue

    ahhhhh.... Man my head hurts with all this table banging.

    I'm having an issue with Sqlite not allowing me to write (UPDATE) to a table that i have just done a SELECT query on because it is "Locked" in a read only state. I have found a few articles on the web about this, but the suggested fix's seem not to work in my case. Any help is so much appreciated. Here's my code. I have commented as much as possible. I have also omitted some error checking code to trim this post.

    You will see at the last db_query() is where its failing. My quetions is... How do i get the UPDATE query to work on table that was just SELECT queried? Thanks much for the help.

    Code:
    /* connect to the database (works) */
    $dbHandle = new PDO('sqlite:'.$CFG->baseroot.'/db/mmt.db3');
    
    function db_query($query) {
    	global $dbHandle;
    
            /* load the query (works) */
    	$handle = $dbHandle->query($query);
    
    	/* check to see if Fetch is necessary (works) */ 
    	if (substr($query,0,6) <> 'UPDATE') {
    
    		/* do the fetch (works) */	
    		$qr = $handle->fetch();
    
    	}
    
            /* fix that releases the lock (doesn't work) */
    	$handle=null;
    
    	return $qr;
    }
    
    /* run the select query (works) */
    $user = db_query("SELECT first_name FROM user WHERE id = '5'");
    
    /* do the if (works) */
    if ($user['name'] == 'John') {
    
    	/* run the update. (this does not work and throws an error) */
    	db_query("UPDATE user SET last_name = 'Doe' WHERE first_name = 'John'");
    }

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I've never used SQLite with that language before but I'd guess you aren't releasing the handle or properly handling running a query despite getting data back.

    Typically though you need to call the finalize function in SQLite to close the recordset. I don't see you ever doing that which is probably why the table is locked.

    Is this PHP you are writing this in?
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •