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...

SAS Secure Tomorrow Free Download

SAS: Secure Tomorrow is a tactical first-person shooter videogame for PC, The player is in command of two SAS units, the main goal of which is to secure the British government from terrorist threat. Meanwhile, a notorious criminal and leader of a terrorist group, escapes with his rescuers from a high security prison. The SAS team is on their toes, and in hot pursuit, to bring him and other terrorists to justice. The SAS team is chasing the fugitives in London, Greenland, and finally in an underground nuclear reactor, and rescuing hostages on the way. Some Screens From SAS Secure Tomorrow Trailer Windows Xp,7,Vista,8 Ram: 512 MB, 1 GB For Vista or Win 7 Video Memory: 128 MB CPU: Intel Pentium 4 2.0 GHz Hard Space: 6 GB Free Download Torrent