Chuyển đến nội dung chính

SQLite first setup

   SQLite is not directly comparable to client/server SQL database engines such as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to solve a different problem.

Client/server SQL database engines strive to implement a shared repository of enterprise data. They emphasis scalability, concurrency, centralization, and control. SQLite strives to provide local data storage for individual applications and devices. SQLite emphasizes economy, efficiency, reliability, independence, and simplicity.

SQLite does not compete with client/server databases.


First configuration for SQLite
     You have to write three js files they are
1) database_startup.js
2) database_query.js
3) database_debug.js

     database_startup.js file helps you to initialize the database, create tables and if you want to insert any default values.


var CreateTb1 = "CREATE TABLE IF NOT EXISTS tbl1(ID INTEGER PRIMARY KEY AUTOINCREMENT, CreatedDate TEXT,LastModifiedDate TEXT, Name TEXT)";
var CreateTb2 = "CREATE TABLE IF NOT EXISTS tbl2(ID INTEGER PRIMARY KEY AUTOINCREMENT, CreatedDate TEXT,LastModifiedDate TEXT,Mark INTEGER)";

var DefaultInsert = "INSERT INTO tbl1(CreatedDate,Name) select '" + new Date() + "','Merbin Joe'  WHERE NOT EXISTS(select * from tbl1)";

var db = openDatabase("TestDB", "1.0", "Testing Purpose", 200000); // Open SQLite Database

$(window).load(function()
{
  initDatabase();
});

function createTable() // Function for Create Table in SQLite.
{

  db.transaction(function(tx)
  {
    tx.executeSql(CreateTb1, [], tblonsucc, tblonError);
    tx.executeSql(CreateTb2, [], tblonsucc, tblonError);

    insertquery(DefaultSettingInsert, defaultsuccess);

  }, tranonError, tranonSucc);
}

function initDatabase() // Function Call When Page is ready.
{
  try
  {
    if (!window.openDatabase) // Check browser is supported SQLite or not.
    {
      alert('Databases are not supported in your device');
    }
    else
    {
      createTable(); // If supported then call Function for create table in SQLite
    }
  }
  catch (e)
  {
    if (e == 2)
    {
      // Version number mismatch.
      console.log("Invalid database version.");
    }
    else
    {
      console.log("Unknown error " + e + ".");
    }
    return;
  }
}
     database_query.js this file is used to control all the insert, delete and update query's and finally it will call the given success function.

function insertquery(query, succ_fun)
{
  db.transaction(function(tx)
  {
    tx.executeSql(query, [], eval(succ_fun), insertonError);
  });
}

function deletedata(query, succ_fun)
{
  db.transaction(function(tx)
  {
    tx.executeSql(query, [], eval(succ_fun), deleteonError);
  });
}

function updatedata(query, succ_fun)
{
  db.transaction(function(tx)
  {
    tx.executeSql(query, [], eval(succ_fun), updateonError);
  });
}

function selectitems(query, succ_fun) // Function For Retrive data from Database Display records as list
{
  db.transaction(function(tx)
  {
    tx.executeSql(query, [], function(tx, result)
    {
      eval(succ_fun)(result.rows);
    });
  });
}
     database_debug.js if any error occur on the transaction or insert or delete or update time the error will through to this files

function tblonsucc()
{
  console.info("Your table created successfully");
}

function tblonError()
{
  console.error("Error while creating the tables");
}

function tranonError(err)
{
  console.error("Error processing SQL: " + err.code);
}

function tranonSucc()
{
  console.info("Transaction Success");
}

function insertonError()
{
  console.error("Error on Insert");
}

function deleteonError()
{
  console.error("Error on delete");
}

function defaultsuccess()
{
  console.info("Default Insert Success");
}

function updateonError()
{
  console.error("Error on update");
}
     You can use the pass the values by following methods.
Select from Table

var query="select Name,CreatedDate  from tbl1 where ID=1";
selectitems(query,getval_success);
-----
-----
function getval_success(result)
{
  if(result.length>0)
 {
  for(var i = 0, item = null; i < result.length; i++)
  {
     item = result.item(i);
     alert(item['Name']);
  }
 }
}
Update values to Table

   var query="update Ex_tb_InExType set IsActive=0 where TypeID="+DelID;
   updatedata(query,select_function);
Insert Value to Table

   var query="insert into tbl1(CreatedDate,Name) values('"+new Date()+"','Joe')";
   insertquery(query,select_function);



Nhận xét

Popular Posts

Autodesk ArtCAM 2017 Crack + Patch + Full Version

Autodesk ArtCAM 2017 Crack + Patch + Full Version ArtCAM is, in fact, a design tool designed more for designers than engineers, and allows designers to showcase their creativity. The software delivers a CNC modeling CAD that can be used for a variety of industries. For example, designers of signs, woodcutters, jewelry designers and many others who can produce high-quality products before they can deliver their products in two-dimensional and three-dimensional form. It's easier with this software than ever. Features and Features of ArtCAM 2017 Crack + Patch + Full Version: Support for 64-bit systems Ability to mount jewelry pieces The ability to create complex shapes Ability to cut different pieces of pages The ability to create floating vectors Save layers and shapes that you must use Availability of ready-made models available System Requirements for ArtCAM 2017 full Crack for 2017, 2018: OS: Windows 7 SP1 / 8.x / 10 for 2012: Processor Type & Speed: Intel Core 2 Duo (or equiv...

Move up and down listbox item using javascript

     In the following example,we can easy to move the list box item up and down using javascript code. Example     function listboxMove(listID, direction) {       var listbox = document.getElementById(listID);       var selIndex = listbox.selectedIndex;       if (-1 == selIndex) {         alert("Please select an option to move.");         return;       }       var increment = -1;       if (direction == 'up')         increment = -1;       else         increment = 1;       if ((selIndex + increment) < 0 ||         (selIndex + increment) > (listbox.options.length - 1)) {  ...

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064)

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064) is a software for creating PDF files. With the help of this software, the user can convert all text files, photos and other relevant documents to PDF format. Adobe Acrobat software is popular with many software users due to the compatibility with most software. You can plan it in the printer's configuration area by typing Ctrl + P keys that are specific to print operations, print texts or anything printable in Adobe Acrobat as a PDF. Adobe Acrobat software features Ability to convert to other formats like images and Word Scan directly from the scanner The presence of internal OCR to identify texts Optimized mode for displaying documents as much as possible Specific compatibility with high-quality images to showcase the best of the documents Ability to do things in a batch like conversion Ability to add new actions to PDF documents Ability to interconnect multiple PDF documents Ability to build form System Requirements Operating Sys...