Jump to content

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.