I am using webdb for client side and I want to match the server side database so the web app can be used offline. This here is the script for creating the server side table…
USE [TESTDB]
GO
/****** Object: Table [dbo].[tblInternalMobile_SalesCalls] Script Date: 01/08/2013 11:30:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblInternalMobile_SalesCalls](
[newcall_id] [int] IDENTITY(1,1) NOT NULL,
[companyname] [varchar](30) NOT NULL,
[comment] [varchar](300) NULL,
[dateUTC] [datetime2](7) NOT NULL,
CONSTRAINT [PK_tblInternalMobile_SalesCalls] PRIMARY KEY CLUSTERED
(
[newcall_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
I am trying to match that on the client side but around identity I get an error Uncaught Error: [object SQLError], Error Code: 5, Error Message: could not prepare statement (1 near "IDENTITY": syntax error)
This is my client side code…
//Database helper
//I want to use a 'namespace' for this to minimize name conflicts
var DbHelper = {};
//use the webDb 'namespace' so we could use indexDb if need be in the future by making a new 'namespace'
DbHelper.webDb = {};
//To store the database object
DbHelper.webDb.db = null;
//Open the database
DbHelper.webDb.openDb = function() {
var dbSize = 5 * 1024 * 1024 //5MB I believe is the max
DbHelper.webDb.db = openDatabase("TestDB", "1.0", "Database manager", 5 * 1024 * 1024);
};
//If there is an error its going to throw an error, I havent added any handlers for this, so it should be added where the function is called
DbHelper.webDb.onError = function(tx, e) {
throw "Error: " + e + ", Error Code: " + e.code + ", Error Message: " + e.message;
};
//This should be used if we are doing a non query, where we dont need to use the result
DbHelper.webDb.onSuccess = function(tx, r) {
console.log("Database result: " + r)
};
//Create the tables we are going to use
DbHelper.webDb.createTables = function() {
var db = DbHelper.webDb.db;
db.transaction(function(tx) {
//I am using the same tables as I am on the server, so we dont confuse things
tx.executeSql("CREATE TABLE IF NOT EXISTS tblInternalMobile_SalesCalls(newcall_id INT PRIMARY KEY IDENTITY(1,1), companyname varchar(30), comment varchar(300), dateUTC DATETIME2)", [], DbHelper.webDb.onSuccess, DbHelper.webDb.onError);
});
}
DbHelper.webDb.init = function() {
DbHelper.webDb.openDb();
DbHelper.webDb.createTables();
}
$(document).ready(function(e) {
DbHelper.webDb.init();
});
Why the heck isn’t it working? I believe I am using the right syntax, obviously not.
The problem is this statement CREATE TABLE IF NOT EXISTS tblInternalMobile_SalesCalls(newcall_id INT PRIMARY KEY IDENTITY(1,1), companyname varchar(30), comment varchar(300), dateUTC DATETIME2)
If you’re actually using SQL Server or one of its variants on the client side as well as on the server side, you should probably use the same CREATE TABLE statement. But SQL Server doesn’t support CREATE TABLE IF NOT EXISTS.
If you’re using the deprecated HTML5 interface, your user agent is expected to conform to SQLite syntax.
I believe w3.org’s Indexed Database API is intended to supersede webdb.