Phonegap:在javascript中无法访问在java中创建的表。 [英] Phonegap: Table created in java is not accessible in javascript.

查看:49
本文介绍了Phonegap:在javascript中无法访问在java中创建的表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我需要在java中创建Sqlite表,并且必须从javascript访问它。表是通过java创建的,但在javascript中访问它时显示表未找到。请帮我。非常迫切。



这是我的代码。在下面的代码我正在创建数据库。我在addData()中将数据插入到表callLog中。我从扩展DroidGap的类调用函数 addData 。创建数据库和表,甚至表中也有值。我使用 SQLite数据库浏览器探索了数据库。

hi friends,
I need to create Sqlite table in java and have to access it from javascript. Table is created through java but while accessing it in javascript it shows table is not found. Please help me. Its very urgent.

Here is my code. in the below code i am creating database. I am INSERTING data into table "callLog" in addData(). I am calling the function addData from the class which extends "DroidGap". Database and table are created and even there are values inside the table. I explored the database using SQLite Database Browser.

package android.service.DBSample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class database extends SQLiteOpenHelper  {
	
	public static final String TABLE_COMMENTS = "callLog";
	  public static final String COLUMN_ID = "Id";
	  public static final String number = "Number";

	  private static final String DATABASE_NAME = "calllogs";
	  private static final int DATABASE_VERSION = 1;

	  private static final String DATABASE_CREATE = "create table "+ TABLE_COMMENTS + "(" + COLUMN_ID+ " integer primary key autoincrement, " + number + " text not null);";
	  
	public database(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	  public void onCreate(SQLiteDatabase database) {
	    database.execSQL(DATABASE_CREATE);
	    int callCount=0;
		  Log.d("CallLog_Plugin", "Table Creation:"+ callCount);
	  }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		 int callCount=0;
		  Log.d("CallLog_Plugin", "Table Updation:"+ callCount);
		    //db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
		   // onCreate(db);
	}
	
	public void addData(calllogs callogs)
	{
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues values=new ContentValues();
		values.put(number, callogs.getNumber());
		db.insert(TABLE_COMMENTS,null,values);
		db.close();
		int callCount=0;
		  Log.d("CallLog_Plugin", "Insertion:"+ callCount);
	}
}







package android.service.DBSample;

public class calllogs {

	String Number;
	
	public calllogs(){
        
    }
    // constructor
    public calllogs(String num){
        this.Number = num;
    }
	
	public String getNumber(){
        return this.Number;
    }
}





以下是我用来调用addData()的代码。



The following is the code i used to call addData().

database=new database(this);
database.addData(new calllogs("344597587"));





在javascript中,我试图访问 callLog 表,但我得到错误在编译期间找不到表。这里有javascript,





In javascript i tried to access the callLog table but i got the error Table not found during compiling. And here comes the javascript,

var db=window.openDatabase("calllogs","1.0","calllogs",200000);
        		db.transaction(function(tx){
        		tx.executeSql('SELECT * FROM callLog', [], function (tx, results) 
         		{
            		var len = results.rows.length;
            		alert(len);
        		});
        		});

推荐答案

嗨。再次。我正在为我自己的问题找到解决方案。没有人试图回答这个问题。我在给出解决方案。



Javascript和java在不同位置创建数据库,因此我们无法访问在Javascript中用Java创建的数据库或反之亦然。因此,有一个名为 SQLitePlugin 的phonegap插件。使用此插件,我们可以在同一位置创建数据库。这个插件只是在Java创建数据库的位置创建数据库。因此可以从Javascript和Java访问相同的数据库。



一旦你有了插件,用下面给出的更改用于打开或创建数据库的代码,



var db = window.openDatabase(database_name,version,Definition,size);



var db = window.sqlitePlugin .openDatabase(database_name,version,Definition,size);



使用上面的代码,我们可以访问在java中创建的数据库。



该插件可在以下链接中找到。这个链接中详细介绍了一步一步的信息。



SQLitePlugin



谢谢。
Hi. Again. I am with the solution for my own question. As nobody tried to answer this question. I am giving solution.

Javascript and java create database in different locations so we can not access DB created in Javascript in java or viceversa. So, there is a phonegap plugin named SQLitePlugin. Using this plugin, we can create the database in the same location. This plugin just creates the DB in location where Java creates the DB. So same database can be accessed from Javascript and Java.

Once you have the plugin, Change the code used to open or create the database in javascript as given below,

var db=window.openDatabase("database_name","version","Definition","size");

var db=window.sqlitePlugin.openDatabase("database_name","version","Definition","size");

Using the above code, we can access the database created in java also.

The plugin is available in the following link. Step by step information is given in a detail way in this link.

SQLitePlugin

Thanks.


这篇关于Phonegap:在javascript中无法访问在java中创建的表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆