7

How can I create a database in sqlite from JavaScript?

2
  • If you ask SQLite to open a database file that doesn't exist, when you start creating tables in it, the file will be created. What specifically is your question? Commented Feb 24, 2011 at 11:01
  • And another question: what platform? iphone? android? normal (win/linux) server? Commented Feb 24, 2011 at 11:03

4 Answers 4

7

May this script help you.

<script type="text/javascript">
function createDatabase(){
    try{
     if(window.openDatabase){
             var shortName   =  'db_edentiti';
             var version   =  '1.0';
             var displayName  =  'Edentiti Information';
             var maxSize   =  65536; // in bytes
             db    =  openDatabase(shortName, version, displayName, maxSize);
                    alert('Sqlite Database created');
         }
    }catch(e){
     alert(e);
    }
 }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Where is this DB located exactly?
1

For what you want to use sqlite? in firefox you could get extension sqlite manager so there you could create database and tables...but if you are developing on ios you have to create entities in xcode

hope it helps Wblade

++so I suggest you to use AJAX instead Javascript or php

I find:

<?php
$db = new PDO('sqlite:/usr/local/zodiac');$db->beginTransaction();
$q = $db->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'zodiac'");if ($q->fetch() === false) {    $db->exec(<<<_SQL_
CREATE TABLE zodiac (
  id INT UNSIGNED NOT NULL,
  sign CHAR(11),
  symbol CHAR(13),
  planet CHAR(7),
  element CHAR(5),
  start_month TINYINT,
  start_day TINYINT,
  end_month TINYINT,
  end_day TINYINT,
  PRIMARY KEY(id)
)
_SQL_
);    $sql=<<<_SQL_
INSERT INTO zodiac VALUES (1,'Aries','Ram','Mars','fire',3,21,4,19);
INSERT INTO zodiac VALUES (2,'Taurus','Bull','Venus','earth',4,20,5,20);
INSERT INTO zodiac VALUES (3,'Gemini','Twins','Mercury','air',5,21,6,21);
_SQL_;    foreach (explode("\n",trim($sql)) as $q) {
        $db->exec(trim($q));
    }
    $db->commit();
} else {
    $db->rollback();
}
?>

2 Comments

i wan't to create in javascript platform for saving data offline.
Do you work in Adobe air enviroment?because it is the only enviroment i know which use javascript and sqlite
0

Hi There are multiple ways to create a db using SQLite, as you know SQLite is very lightweight and very flexible of datastorage, the commands also are very easy to remember.

I am a Windows XP User.

In my system i have extracted the SQLite engine 3.X to C:\Ronald\Personal\epmc\JavaJ2EE

u can some Editor like SQLiteBrowser, its very nice to work with. no extra framwork needed.

Open a command prompt and point to the SQLite engine.

In my case it was C:\Ronald\Personal\epmc\JavaJ2EE

give the following command to create a DB

          sqlite3.exe TestDB.db;

Once u give this automatically the database file will be created in the particular location. Then start creating tables like what you have done in Oracle or MySQL.

Thanks Ronald Reagan Nathan

Comments

-1

You can try the SQLite administrator:

http://sqliteadmin.orbmu2k.de/

it is a good tool to have around anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.