Author Topic: [SOLVED] SQLite - Can't create table (I think?)  (Read 1276 times)

0 Members and 1 Guest are viewing this topic.

Offline CatfishTopic starter

  • Enthusiast
  • Posts: 352
    • View Profile
    • beardedDonkey
[SOLVED] SQLite - Can't create table (I think?)
« on: April 06, 2009, 04:30:35 AM »
Hi all,

I'm newto SQLite but want to learn howto use it with PHP.

I've been writing a bit of a frontend for it in PHP, currently I make my script look for database files, list them, then the user can choose a database, then it looks for tables in that database, if there's none the user can make one, define the fields etc. and hit submit to create the table.

I am just going off a tutorial I found online:
http://devzone.zend.com/node/view/id/760
and i'm using the OO approach in my code.

My code that sends the table creation query to the database is:
Code: [Select]
case 'submitTable':
{
if (empty($localPostGet['dbName']) || empty($localPostGet['tableName']))
continue;
// build SQL query to send to database
$dbQuery .= 'BEGIN; CREATE TABLE '.$localPostGet['tableName'].'(';
for ($i = 1; $i <= count($localPostGet['fieldName']); $i++)
{
if (!empty($localPostGet['fieldName'][$i]))
$dbQuery .= $localPostGet['fieldName'][$i].' '.$localPostGet['fieldType'][$i].$localPostGet['fieldExtras'][$i];
if (isset($localPostGet['fieldName'][$i + 1]) && !empty($localPostGet['fieldName'][$i + 1]))
$dbQuery .= ', ';
}
$dbQuery .= '); COMMIT;';
$o .= '<pre>Query sent:'."\n".$dbQuery.'</pre><br />'."\n";
if ($db->query($dbQuery))
$o .= 'Successfully made table <i>'.$localPostGet['tableName'].'</i> in database <i>'.$localPostGet['tableName'].'</i><br />'."\n";
else
$o .= 'Problem: Could not make table <i>'.$localPostGet['tableName'].'</i> in database <i>'.$localPostGet['tableName'].'</i><br />'."\n";
$o .= 'Return to <a href="'.$_SERVER['PHP_SELF'].'">select database</a> page.<br/>'."\n";
break;
}

The values that pass to that section of code are as follow:
Code: [Select]
Array ($localPostGet)
(
    [tableName] => testTable
    [action] => submitTable
    [dbName] => testDB.sqlite
    [fieldName] => Array
        (
            [1] => test
            [2] => id
        )

    [fieldType] => Array
        (
            [1] => TEXT
            [2] => INTEGER
        )

    [fieldExtras] => Array
        (
            [1] =>
            [2] =>  PRIMARY KEY
        )

)

The query being sent is:
BEGIN; CREATE TABLE testTable(test TEXT, id INTEGER PRIMARY KEY); COMMIT;

Is my checking the outcome of $db->query with an if() valid enough to test if the query was successful? I know with PHP's mysql_ functions you sometimes need to use certain functions to check things if they worked.

Some help on this would be most appreciated. :)
This signature is not a massive image that detracts from the forum's purpose.
Standard Disclaimer - Any code or help I post is 'as is'. I don't know if my code will work or even if it's correct. It's free isn't it?
My PHP Tutorials

Offline CatfishTopic starter

  • Enthusiast
  • Posts: 352
    • View Profile
    • beardedDonkey
Re: SQLite - Can't create table (I think?)
« Reply #1 on: April 07, 2009, 09:08:42 AM »
Seem to have fixed this by using $db->queryExec($dbQuery); instead of $db->query();

None of the tutorials I've seen online stress anything about having to use queryExec().
This signature is not a massive image that detracts from the forum's purpose.
Standard Disclaimer - Any code or help I post is 'as is'. I don't know if my code will work or even if it's correct. It's free isn't it?
My PHP Tutorials