Catfish Posted April 6, 2009 Share Posted April 6, 2009 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: 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: 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. Link to comment https://forums.phpfreaks.com/topic/152757-solved-sqlite-cant-create-table-i-think/ Share on other sites More sharing options...
Catfish Posted April 7, 2009 Author Share Posted April 7, 2009 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(). Link to comment https://forums.phpfreaks.com/topic/152757-solved-sqlite-cant-create-table-i-think/#findComment-803449 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.