1

我将 webdb 用于客户端,我想匹配服务器端数据库,以便可以离线使用 Web 应用程序。这是用于创建服务器端表的脚本...

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

我试图在客户端匹配它但围绕身份我得到一个错误Uncaught Error: [object SQLError], Error Code: 5, Error Message: could not prepare statement (1 near "IDENTITY": syntax error)

这是我的客户端代码...

//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();
});

为什么它不起作用?我相信我使用了正确的语法,显然不是。

问题是这个声明CREATE TABLE IF NOT EXISTS tblInternalMobile_SalesCalls(newcall_id INT PRIMARY KEY IDENTITY(1,1), companyname varchar(30), comment varchar(300), dateUTC DATETIME2)

4

1 回答 1

3

如果您实际上在客户端和服务器端使用 SQL Server 或其变体之一,您可能应该使用相同的 CREATE TABLE 语句。但是 SQL Server 不支持 CREATE TABLE IF NOT EXISTS

如果您使用弃用的 HTML5 界面,则您的用户代理应符合 SQLite 语法。

CREATE TABLE tblInternalMobile_SalesCalls(
  newcall_id INTEGER PRIMARY KEY AUTOINCREMENT, 
  companyname varchar(30), 
  comment varchar(300), 
  dateUTC DATETIME2
);

我相信 w3.org 的索引数据库 API旨在取代 webdb。

于 2013-01-07T22:51:59.243 回答