简体   繁体   English

JavaScript SQLite HTML5

[英]JavaScript SQLite Html5

I found a lot of documentation to create a local database (SQLite) in javascript and it works perfectly. 我发现了很多使用javascript创建本地数据库(SQLite)的文档,并且效果很好。 But now I want the database not to be created, but open directly from a directory, for example (C: \\ Users ......). 但是现在我不希望创建数据库,而是直接从目录打开它,例如(C:\\ Users ......)。 Can I do it automatically? 我可以自动做到吗?

This is the JavaScript to create the database 这是创建数据库的JavaScript

    var localDB = null;
    var CreateTb1 = "CREATE TABLE ......";
    var CreateTb2 = "CREATE TABLE ......";

    function onInit(){
    try {
        if (!window.openDatabase) {
            updateStatus("Erro: Seu navegador não permite banco de dados.");
        }
        else {
            initDB();
            createTables();
            //queryAndUpdateOverview();
        }
    } 
    catch (e) {
        if (e == 2) {
            updateStatus("Erro: Versão de banco de dados inválida.");
        }
        else {
            updateStatus("Erro: Erro desconhecido: " + e + ".");
        }
        return;
    }
}

    function initDB(){
    var shortName = 'database.db';
    var version = '1';
    var displayName = 'database.db';
    var maxSize = 65536; // Em bytes
    localDB = window.openDatabase(shortName, version, displayName, maxSize);
}

    function createTables(){

    localDB.transaction(function(transaction)
  {
    transaction.executeSql(CreateTb1, [], nullDataHandler, errorHandler);
    transaction.executeSql(CreateTb2, [], nullDataHandler, errorHandler);

  }, tranonError, tranonSucc);


}

JavaScript code running in a website in a browser cannot access files on the local drive of the user. 在浏览器的网站中运行的JavaScript代码无法访问用户本地驱动器上的文件。 That would be a huge security risk. 那将是巨大的安全风险。

If you want a user to be able to use the data in a local file, you have to provide a mechanism for them to upload the file to your server where you can then access the data. 如果希望用户能够使用本地文件中的数据,则必须提供一种机制,让他们将文件上传到服务器,然后可以在其中访问数据。

Note, that you cannot write back the data to the user's disk. 请注意,您不能将数据写回到用户磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM