Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
Jamroom Developers:
schema.php
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 09/06/09 01:11 
So in the schema.php file(of my custom module), if i wanted to create a table and populate it with some sample data. Would I do something like this:

Code

$tbl = "CREATE TABLE {$jamroom_db['jrSampleTable']} (
sample_id INT(7) UNSIGNED NOT NULL  AUTO_INCREMENT PRIMARY KEY,
sample_title VARCHAR(100) NOT NULL DEFAULT 'Sample Title');
INSERT INTO {$jamroom_db['jrSampleTable']} (
sample_id, sample_title) values (1, 'title1');
INSERT INTO {$jamroom_db['jrSampleTable']} (
sample_id,sample_title) values (2, 'title2')";
dbVerifyTable($jamroom_db['jrSampleTable'],$tbl);


Or would I need to do something like this:

Code

$tbl = "CREATE TABLE {$jamroom_db['jrSampleTable']} (
sample_id INT(7) UNSIGNED NOT NULL  AUTO_INCREMENT PRIMARY KEY,
sample_title VARCHAR(100) NOT NULL DEFAULT 'Sample Title',
)";

dbVerifyTable($jamroom_db['jrSampleTable'],$tbl);

$dta = "INSERT INTO {$jamroom_db['jrSampleTable']} (
sample_id, sample_title) VALUES (1, 'title1'),
(2, 'title2')";
dbQuery($dta);


Or should I do it somewhere else?

Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 09/06/09 08:51 
You want to do it the second way - the first way will not work.

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 09/06/09 18:00 
Awesome, thanks. One more question, Like the Table Definitions, would the sample data need to be placed on its own line? Or is that just for the dbVerifyTable Function?

Ie:


Code

$req = "INSERT INTO {$jamroom_db['sampletable']} (
sample_id,
sample_name
) VALUES (
1,
'Name 1'
), (
2,
'Name 2'
)";
dbQuery($req);


Or can i declare it in the same line?

Code

$req = "INSERT INTO {$jamroom_db['sampletable']} (
sample_id, sample_name ) VALUES (1, 'Name 1'), (2, 'Name 2')";
dbQuery($req);

Thanks again for your response.

Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 09/07/09 14:20 
No - you don't need to do your "normal" SQL queries with each column on a separate line - your second example is correct.

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 09/10/09 21:11 
When dbVerifyTable returns true, does that mean that the table already exists or that it was just created? If I want to populate a table with some sample data, and did it the way mentioned above, wouldn't the sample data be INSERTed to the table, each time the Integrity Check is ran?

Would it be best to do it the following way(Provided that dbVerifyTable returned true if created?

Code

$tbl = "CREATE TABLE {$jamroom_db['jrSampleTable']} (
sample_id INT(7) UNSIGNED NOT NULL  AUTO_INCREMENT PRIMARY KEY,
sample_title VARCHAR(100) NOT NULL DEFAULT 'Sample Title',
)";

$result = dbVerifyTable($jamroom_db['jrSampleTable'],$tbl);

if ($result == true) {
$dta = "INSERT INTO {$jamroom_db['jrSampleTable']} (
sample_id, sample_title) VALUES (1, 'title1'),
(2, 'title2')";
dbQuery($dta);
}


Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 09/11/09 13:27 
dbVerifyTable() returns true when it successfully validates the table - it does not check in any way for actual data in the table. So what you need to do is:


Code

$req = "SELECT * from your_table LIMIT 1";
$_rt = dbQuery($req,'SINGLE');
if (!isset($_rt) || !is_array($_rt)) {
    .. table has no data .. do your insert ..
}


Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 09/11/09 16:12 
Thats how I ended up writing it. Just figured if I was able to get a result, on creation, I could save a call to the database. Not that the Query is very time intensive. Thanks again.

Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
Jamroom Developers

 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.