【问题标题】:phonegap sqlite no such table foundphonegap sqlite 没有找到这样的表
【发布时间】:2014-05-05 14:56:43
【问题描述】:

我正在 Eclipse IDE 中使用 Phonegap 开发一个使用 sqlite 作为我的数据库的 Android 应用程序。 在我的 index.html 中,我正在使用 db.openDatabase 创建数据库。在 data>data>com.app>app_storage> 下的文件资源管理器中创建一个数据库 00001.db 在同一页面中,我正在创建我的表(总共 7 个)并填充其中一个。 当我导航到不同的页面并尝试查询表时,我在 logCat 中收到错误提示“没有这样的表” 为什么会这样?我该如何解决 ?

我看到 SQLiteLog 没有这样的表 cdpportal_employee_details

          <!DOCTYPE html>
         <html>
     <head>



    <title>Embedded Sql Example</title>



    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="jquery.min.js"></script>




    <script type="text/javascript" charset="utf-8">


        var db;
        var shortName = 'WebSqlDB';
        var version = '1.0';
        var displayName = 'WebSqlDB';
        var maxSize = 65535;
        role="false";

        function errorHandler(transaction, error) {
           alert('Error: ' + error.message + ' code: ' + error.code);
        }


        function successCallBack() {
           alert("DEBUGGING: success");
        }

        function nullHandler(){alert ("TABLE CREATED");}


        function onBodyLoad(){


            alert("DEBUGGING: we are in the onBodyLoad() function");

            if (!window.openDatabase) {

                alert('Databases are not supported in this browser.');
                return;
            }


            db = openDatabase(shortName, version, displayName,maxSize);

            db.transaction(function(tx){

            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_employee_details(employee_id TEXT PRIMARY KEY,password TEXT,role TEXT,employee_name TEXT,contact_number NUMBER,email_id TEXT)');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_course_details(course_id TEXT PRIMARY KEY NOT NULL,course_name TEXT NOT NULL,start_date TEXT NOT NULL,end_date TEXT NOT NULL,sess TEXT,instructor_name TEXT,techassociate_name TEXT,venue TEXT)');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_attendance_code_gen(course_id TEXT,day TEXT,attendance_code TEXT,FOREIGN KEY (course_id) REFERENCES cdpportal_course_details(course_id),PRIMARY KEY (course_id,day,attendance_code) )');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_registered_emp(employee_id TEXT ,course_id TEXT ,FOREIGN KEY (employee_id) REFERENCES cdpportal_employee_details(employee_id),FOREIGN KEY (course_id) REFERENCES cdpportal_course_details(course_id),PRIMARY KEY(employee_id, course_id))');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_removed_courses(admin_id TEXT,course_id TEXT,course_name TEXT,FOREIGN KEY (admin_id) REFERENCES cdpportal_employee_details(employee_id),FOREIGN KEY (course_id) REFERENCES cdpportal_course_details(course_id),PRIMARY KEY (admin_id, course_id))');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_attendance_record(employee_id TEXT,course_id TEXT,day TEXT,attended TEXT,FOREIGN KEY (employee_id) REFERENCES cdpportal_employee_details(employee_id),FOREIGN KEY (course_id) REFERENCES cdpportal_course_details(course_id),PRIMARY KEY (employee_id, course_id))');
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS cdpportal_assesment_result(employee_id TEXT,course_id TEXT ,handson_marks NUMBER,objective_marks NUMBER,FOREIGN KEY (employee_id) REFERENCES cdpportal_employee_details(employee_id),FOREIGN KEY (course_id) REFERENCES cdpportal_course_details(course_id),PRIMARY KEY(course_id,employee_id))');
            tx.executeSql('insert into cdpportal_emploee_details values ("rifath","Qwerty","E","Sri Harsha L",9633852741,"sriharsha@infosys.com")');
            },nullHandler,successCallBack);


        }
function fun()
{

var uname=document.getElementById("usrnm").value;
var pwd=document.getElementById("pswd").value;
db = openDatabase(shortName, version, displayName,maxSize);
db.transaction(function(tx){
    tx.executeSql( 'SELECT role from cdpportal_employee_details where employee_id=? AND        password=?',[uname, pwd], function(tx, results)
    {
        role=results.value;

    }, null);
});
alert(role);

}


    </script>
</head>


<body onload="onBodyLoad()">

    <h1>WebSQL</h1>
    <form>
  <div>
      <h3>Login</h3>
      <label for="usrnm" class="ui-hidden-accessible">Username:</label>
      <input type="text" name="user" id="usrnm" placeholder="Username">
      <label for="pswd" class="ui-hidden-accessible">Password:</label>
      <input type="password" name="passw" id="pswd" placeholder="Password">
      <input type="submit" data-inline="true" value="Log in" onclick= fun() style="color : teal; background-color: #FFFFC0"> 
    </div>
 </form>

</body>

(1) 没有这样的表:cdpportal_emploee_details

【问题讨论】:

  • 我在 Google 上搜索了使用带有 phonegap 的 sqlite 的示例代码,并遇到了许多说明他们正在使用 sqlite 的示例。如何使用 sqlite 插件?一步一步的方法将不胜感激
  • 对不起,我刚刚注意到一个拼写错误,但现在它抛出 (23) not authorized
  • 我正在使用 Eclipse 构建:v21.0.1-543035。请帮助我几乎放弃了希望
  • 查看我编辑的答案,我相信一个简单的错字是您问题的原因

标签: android sqlite cordova


【解决方案1】:

在继续您的问题之前,您确定您使用的是sqlite database,请参阅 phonegap 文档

This API is based on the W3C Web SQL Database Specification and W3C Web Storage API Specification. Some devices already provide an implementation of this spec. For those devices, the built-in support is used instead of replacing it with PhoneGap's implementation. For devices that don't have storage support, PhoneGap's implementation should be compatible with the W3C specification.

更多详情here

对于 sqlite 的 AFAIK,您需要使用插件,但 websql 是 phonegap 原生支持的东西,因此,据我了解,您使用的是 web-sql 而不是 sqlite

现在,回答您的问题,我不确定您是否遇到了我曾经遇到过的问题,这可能是错误的,但我仍然遇到了同样的问题,实际上创建的数据库就像saldi.db.db,我正在寻找对于saldi.db,这实际上创建了一个新数据库,因此您可以尝试清除此数据库/卸载应用程序,然后在触发任何查询以保存或从表中获取数据之前检查此文件,只需确保创建了正确的数据库

干杯

【讨论】:

  • 你试过这个建议了吗?
  • 我确实尝试安装插件。但是在 SQLitePlugin.java 中显示了一个编译错误,说最低版本是 8,要求是 17。我正在一个可以为我提供最好版本 8 的机构实习。您能否提供有关如何使用该插件的教程的任何链接?请记住,我是新手,正在使用 Eclipse Classic IDE
  • 这里不需要 sqlite 插件,你可以使用 web-sql,你可以在这里查看代码gist.github.com/liamartinez/3791512,如果这有帮助你可以考虑投票/接受答案跨度>
【解决方案2】:

当您导航到该页面时,您必须确保该表存在。所以看看你在哪里完成了 CREATE table IF NOT EXISTS 语句。

例如,如果 create 语句位于函数内部,请检查该函数是否已在该特定页面上调用。

编辑:我发现了你的问题:

tx.executeSql('insert into cdpportal_emploee_details values ("rifath","Qwerty","E","Sri Harsha L",9633852741,"sriharsha@infosys.com")');

您在 cdpportal_emploee_details 中缺少“y”

【讨论】:

    【解决方案3】:

    我才刚刚开始与 jQuery、Cordova/PhoneGap 和 SQLite 进行斗争,但一种可能是您遇到了竞争状况。 SQLite 插件在后台异步运行,因此您的页面可能在这些事务完成之前加载。尝试在 Promise 中插入您的 openDatabase 调用,以便您只有在确定它已创建后才能使用它。

    function initDB()
    {
        var promLoaded = $.Deferred() ; // creates a promise
    
        db = sqlitePlugin.openDatabase(/* your DB params; should be JSON obj for plugin */) ;
        db.transaction(
            function(tx) // initial transaction request
            { tx.executeSql(/* your CREATE TABLE statement */) ; }
            ,
            function(err) // error callback
            { console.log(err) ; } 
            ,
            function(tx,rs) // success callback
            { // chain a pre-population query off the success of the table creation
                db.transaction(
                    function(tx)
                    { tx.executeSql(/* INSERT statement to pre-populate data*/ ) ; }
                    ,
                    function(err)
                    { console.log(err) ; }
                    ,
                    function(tx,rs)
                    {
                        console.log( "Tables ready." ) ;
                        promLoaded.resolve() ; // promise resolves when stuff is complete
                    }
                );
            }
        );
    
        return promLoaded.promise() ; // return the promise to the caller
    }
    

    使用此方案,您应该能够在initDB().done() 执行后立即开始使用该表。

    如果你传递了这个承诺,那么你可以稍后检查它的状态并等待你的SELECT,直到你知道承诺已经解决。

    我自己才刚刚开始学习这个,在我自己的应用程序中正确地实现了这个,并且几乎没有开始测试它,所以我希望上面没有严重的错误。 YMMV。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 2013-03-07
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      相关资源
      最近更新 更多