Click here to Skip to main content
15,886,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
Java
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);
	}
}


Java
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;
    }
}


The following is the code i used to call addData().
Java
database=new database(this);
database.addData(new calllogs("344597587"));


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

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);
        		});
        		});
Posted
Updated 18-Aug-13 19:03pm
v3
Comments
ZurdoDev 17-Aug-13 9:10am    
How can we help? I can't see anything you have.
Annamalai09 19-Aug-13 1:04am    
i have updated the code.. Pl help me now.
H.Brydon 18-Aug-13 23:11pm    
Posting an elementary question with no code and saying it is urgent is a recipe for getting no respect, no help and/or no response. I'm feeling nice today and am going out of my way by being helpful and redundant here.
Annamalai09 19-Aug-13 1:05am    
hi brydon, i have put my code here. Have a look at this and tell me what am i supposed to do.??
Annamalai09 23-Aug-13 5:57am    
Anyone reply for the question. I am still struggling to find out the answer.

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900