如何使用javascript从本地数据库sqlite文件读取表数据?

时间:2018-07-16 23:29:22

标签: javascript sqlite sql.js

我知道可以使用sql.js,但是代码似乎不起作用?我将输入标签和文件选项以及所有handleFiles(this.files)用于onchange事件。

JavaScript:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.irhad.mytestapplication.data.Podaci;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;

public class MainActivity extends AppCompatActivity {

    Button btnSkenirajQR;
    Button btnOpcije;
    Button btnExit;
    TextView txtIspis;
    boolean baterija_ispisano = false;

    BroadcastReceiver batteryInfo = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            if(level <= 10 && !baterija_ispisano){
                Toast.makeText(getApplicationContext(),
                        "Molim vas napunite bateriju (<10%)",Toast.LENGTH_LONG);
                        baterija_ispisano = true;
            }else if(level >= 11 && baterija_ispisano){
                baterija_ispisano = false;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSkenirajQR = findViewById(R.id.btnSkener);
        btnOpcije = findViewById(R.id.btnOpcije);
        btnExit = findViewById(R.id.btnExit);
        txtIspis = findViewById(R.id.txtIspis);

    }

    public void open_Skener(View view){
        Intent qrskeniranje = new Intent(this, QRActivity.class);
        startActivityForResult(qrskeniranje,0);
        finish();
    }

    public void open_Opcije(View view){
        Intent opcije = new Intent(this, OpcijeActivity.class);
        startActivity(opcije);
        finish();
    }

    public void izadji(View view){
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if(data != null){
             String qr = data.getParcelableExtra("barcode");
             String value = data.getParcelableExtra("vrijednost");
             txtIspis.setText("Barcode vrijednost : " + qr);
         } else {
             txtIspis.setText("Nije ocitalo");
         }
    }
}

function handleFiles(files) {   
      var file = files[0]; 
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      openDbOnFileLoad(reader); 
      }
    function openDbOnFileLoad(reader){
        setTimeout(function () {
        if(reader.readyState == reader.DONE) {
          //console.log(reader.result);
    	  var Uints = new Uint8Array(reader.result);
          db = new SQL.Database(Uints);
          execute("SELECT * FROM TableName LIMIT 1");
        } else {
          //console.log("Waiting for loading...");
          openDbOnFileLoad(reader);
        }
        }, 500);    
      }
    function execute(commands) {
      commands = commands.replace(/\n/g, '; ');
      try {
        var data = db.exec(commands);
        console.log('data query is:'+data);
      } catch(e) {
        console.log('error is:'+e);
      }
    }
    function bin2Array(bin) {
      var i, size = bin.length, ary = [];
      for (i = 0; i < size; i++) {
          ary.push(bin.charCodeAt(i) & 0xFF);
      }
      return ary;
    }

0 个答案:

没有答案