Javascript SQLite Cordova:“;处理SQL时出错:未定义";使用预填充的db
我遵循了这个指南: 我在/assets中有一个名为coor_db.db的预填充数据库 根据指南,我修改了主要活动文件的代码:Javascript SQLite Cordova:“;处理SQL时出错:未定义";使用预填充的db,javascript,mysql,cordova,Javascript,Mysql,Cordova,我遵循了这个指南: 我在/assets中有一个名为coor_db.db的预填充数据库 根据指南,我修改了主要活动文件的代码: public class GoogleMaps extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set by &l
public class GoogleMaps extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
try {
File dbFile = getDatabasePath("coor_db.db");
if(!dbFile.exists()){
this.copy("coor_db.db", dbFile.getAbsolutePath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
String parentPath = CheckDirectory.getParent();
File filedir = new File(parentPath);
if (!filedir.exists()) {
if (!filedir.mkdirs()) {
return;
}
}
InputStream in = this.getApplicationContext().getAssets().open(file);
File newfile = new File(folder);
OutputStream out = new FileOutputStream(newfile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close();
out.close();
}
}
公共类GoogleMaps扩展了Cordova活动
{
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//在config.xml中设置
loadUrl(launchUrl);
试一试{
File dbFile=getDatabasePath(“coor_db.db”);
如果(!dbFile.exists()){
copy(“coor_db.db”,dbFile.getAbsolutePath());
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
无效副本(字符串文件、字符串文件夹)引发IOException{
文件检查目录;
CheckDirectory=新文件(文件夹);
字符串parentPath=CheckDirectory.getParent();
File filedir=新文件(父路径);
如果(!filedir.exists()){
如果(!filedir.mkdirs()){
回来
}
}
InputStream in=this.getApplicationContext().getAssets().open(文件);
File newfile=新文件(文件夹);
OutputStream out=新文件OutputStream(新文件);
字节[]buf=新字节[1024];
内伦;
而((len=in.read(buf))>0)out.write(buf,0,len);
in.close();
out.close();
}
}
然后我创建了一个js文件,尝试在其中打开它,然后尝试进行查询
var coor_db = (function (){
function init(){
var db = window.openDatabase("coor_db", "1.0", "coor_db", 200000);
db.transaction(queryDB, errorCB);
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
//select all from coor_db
function queryDB(tx){
tx.executeSql('SELECT * FROM coor_db', [], querySuccess, errorQuery);
}
function errorQuery(err) {
alert("Error processing SQL query: "+err.code);
}
//handle the result
function querySuccess(tx, result){
alert("I'm called: "+result.rows.length);
var array = [];
for(var i = 0; i < result.rows.length; i++){
var row = result.rows.item(i);
alert("id: "+row['id']+" lat: "+row['lat']);
array[i] = {
id: row['id'],
text: row['lat']
}
}
alert(array[2].id+" lat: "+array[2].text);
}
return {
init : init
}
})();
var coor_db=(函数(){
函数init(){
var db=window.openDatabase(“coor_db”,“1.0”,“coor_db”,200000);
数据库事务(查询数据库、错误数据库);
}
函数errorCB(err){
警报(“处理SQL时出错:+err.code”);
}
//从coor_db中选择全部
函数queryDB(tx){
tx.executeSql('SELECT*FROM coor_db',[],querySuccess,errorQuery);
}
函数错误查询(err){
警报(“处理SQL查询时出错:+err.code”);
}
//处理结果
函数查询成功(发送,结果){
警报(“我被呼叫:+result.rows.length”);
var数组=[];
对于(var i=0;i
该错误由方法queryDB(tx)引起。
我试着用coor_db.db来代替coor_db
var db=window.openDatabase(“coor_db”,“1.0”,“coor_db”,200000);
但这并没有改变任何事情
我不知道出了什么事
在我的index.js文件中,我调用了coor_db.init()我遇到了完全相同的问题:(你找到它了吗?错误回调中的err对象怎么可能没有定义?抱歉,我没有遇到这个问题我实际上是通过在官方phonegap文档中记录最常用的名为“tx”的“事务对象”来找到它的。