By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

In memory database

P: n/a
Anyone know of an extension that is an in memory database?

I dont want to create any files on disk (nor install a database engine)
but have an object/variable that points to a database and tables all in
memory. Then to be able to do simple select, insert, deletes and
updates.

This could be easier than using arrays for certain tasks.

Something like this would be nice. E.g.

$db->new memDB('myDB');
$tbl=new memTBL('names')
$tbl->addCol('fname','CHAR',20);
$tbl->addCol('lname','CHAR',20);
$tbl->addCol('dob','DATE');
$tbl->addKey('fname');
$tbl->addKey('lname','fname');
$db->addTable($tbl);

$db->exec("insert into names ('fname',lname','dob') values
('John','Doe','1/1/1900')");
$db->exec("select * from names where fname='John' order by lname");

Thanks

Jun 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ImOk wrote:
Anyone know of an extension that is an in memory database?
I've never used it from PHP, but AIUI SQLite (www.sqlite.org) has a PHP
extension, and can be used to create an in-memory database.
I dont want to create any files on disk (nor install a database engine)
but have an object/variable that points to a database and tables all in
memory.


Why do you want to do this? What problem are you trying to solve? You
are aware that all data may be lost at the end of the script execution?

Tim
Jun 20 '06 #2

P: n/a
Lets say I have an array of names, gender, age and salary that I read
from an Excel spreadsheet or CSV file.

How do I sort this by gender+salary+age and get a subset of anyone over
40 possibly ascending or descending?

E.g.
$arr[0]=array('M',20000,20,'JOHN');
$arr[1]=array('F',22000,24,'JANE');
$arr[2]=array('M',32000,40,'JOE');

I looked at using array_multisort but you have to jump through hoops it
seems. Maybe I am doing this wrong.

I am aware data will be lost of course when the program terminates. And
it can always be saved to a text file. Sometimes small databases are
much faster and easier being manipulated in memory especially if I dont
have to install a database engine.

Tim Martin wrote:
ImOk wrote:
Anyone know of an extension that is an in memory database?


I've never used it from PHP, but AIUI SQLite (www.sqlite.org) has a PHP
extension, and can be used to create an in-memory database.
I dont want to create any files on disk (nor install a database engine)
but have an object/variable that points to a database and tables all in
memory.


Why do you want to do this? What problem are you trying to solve? You
are aware that all data may be lost at the end of the script execution?

Tim


Jun 20 '06 #3

P: n/a

ImOk wrote:
Anyone know of an extension that is an in memory database?


You could use the memory (heap) storage engine in MySQL. You could also
store everything in variables/arrays and do a search in that.

Jun 20 '06 #4

P: n/a

ImOk wrote:
Lets say I have an array of names, gender, age and salary that I read
from an Excel spreadsheet or CSV file.

How do I sort this by gender+salary+age and get a subset of anyone over
40 possibly ascending or descending?

E.g.
$arr[0]=array('M',20000,20,'JOHN');
$arr[1]=array('F',22000,24,'JANE');
$arr[2]=array('M',32000,40,'JOE');

I looked at using array_multisort but you have to jump through hoops it
seems. Maybe I am doing this wrong.

I am aware data will be lost of course when the program terminates. And
it can always be saved to a text file. Sometimes small databases are
much faster and easier being manipulated in memory especially if I dont
have to install a database engine.

Tim Martin wrote:
ImOk wrote:
Anyone know of an extension that is an in memory database?


I've never used it from PHP, but AIUI SQLite (www.sqlite.org) has a PHP
extension, and can be used to create an in-memory database.
I dont want to create any files on disk (nor install a database engine)
but have an object/variable that points to a database and tables all in
memory.


Why do you want to do this? What problem are you trying to solve? You
are aware that all data may be lost at the end of the script execution?

Tim


$arr[0]=array('M',20000,20,'JOHN');
$arr[1]=array('F',22000,24,'JANE');
$arr[2]=array('M',32000,40,'JOE');

function mySort($a1, $a2) {
foreach($a1 as $i => $val) {
if($a1[$i] < $a2[$i])
return -1;
if($a1[$i] > $a2[$i])
return 1;
}
return 0;
}

usort($arr, 'mySort');

Will sort it by the first "column", then the second, then the third,
etc.

Jun 20 '06 #5

P: n/a
>
$arr[0]=array('M',20000,20,'JOHN');
$arr[1]=array('F',22000,24,'JANE');
$arr[2]=array('M',32000,40,'JOE');

function mySort($a1, $a2) {
foreach($a1 as $i => $val) {
if($a1[$i] < $a2[$i])
return -1;
if($a1[$i] > $a2[$i])
return 1;
}
return 0;
}

usort($arr, 'mySort');

Will sort it by the first "column", then the second, then the third,
etc.


Good one. Now I have to fix it to do ascending, descending and up to 3
columns. I was trying to avoid this callback, but I may have no choice.
No one said life was easy.

Jun 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.