Click to See Complete Forum and Search --> : Android SqLite help....


raihan26
April 17th, 2010, 06:13 PM
Can anybody explain me where android store sqLite database....
I have created database and tables on the click event of a Button.... so I just need to verify whether the database and table have been created or not....


I am trying to create database and table on the click event of
button... when I am clicking on the button for creating database and
table... it is saying that process com.example.hw3 has stopped
unexpectedly...please try again... Below is the code... what wrong I
am doing....??
Somebody Please help me....





package com.example.hw3;


import java.util.Locale;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class HW3 extends Activity {
private Button b1;
//private HW3DbAdapter db1;
private EditText text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.EditText01);
initControls();
}

protected void initControls()
{
b1 = (Button)findViewById(R.id.CreateDB);

b1.setOnClickListener(new Button.OnClickListener()
{ public void onClick (View v){ DoWork(); }});
}

protected void DoWork()
{

SQLiteDatabase db;

// db1 = new HW3DbAdapter(this);
text.setText("Button 1 was clicked");
// db1.open();
//fillData();

db = openOrCreateDatabase(
"db_hw3.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_COUNTRIES =
"CREATE TABLE db_entry ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "mb_text TEXT NOT NULL,"
+ "ph_location TEXT);";

db.execSQL(CREATE_TABLE_COUNTRIES);


}



}

Xeel
April 19th, 2010, 04:26 PM
By default the dbs are in /data/data/{package_name}/

read this for more: http://developer.android.com/guide/topics/data/data-storage.html

raihan26
April 20th, 2010, 05:24 PM
Thanks for replying...
I am having one more problem now...
I am using this below java file to create a database and table on the click event of Create DB Button( till here it is working fine) and then below this Create DB button, there is one more button named Send Button... and on the click event of Send Button I am opening another activity which contains a edit text and a button called Insert(this is also working fine for me) and on the click event of this Insert Button... I have to insert the value that we are typing into edit text box into the table that we have created previously.... but I am not able to figure it out why it is not inserting into table that we have created previously... what wrong I am doing please let me know.... Below are the codes....
-----------------
This Code contains Create DB Button and Send Button and click event also that are creating tables and database


package com.example.hw3;


import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class HW3 extends Activity {
private Button b1;
private Button b2;
@SuppressWarnings("unused")
private A1 test;
private EditText text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.EditText01);
initControls();
}

protected void initControls()
{
b1 = (Button)findViewById(R.id.CreateDB);
b2 = (Button)findViewById(R.id.Button02);





b1.setOnClickListener(new Button.OnClickListener()
{ public void onClick (View v){ DoWork(); }});

b2.setOnClickListener(new Button.OnClickListener()
{ public void onClick (View v){
Intent myIntent = new Intent(HW3.this, A1.class);
// HW3.this.startActivity(myIntent);
startActivity(myIntent);
}});


}


protected void DoWork()
{

// SQLiteDatabase db=null;

// db1 = new HW3DbAdapter(this);
text.setText("Button 1 was clicked");
// db1.open();
//fillData();

SQLiteDatabase myDB= null;
String TableName = "db_entry";

/* Create a Database. */
try {
myDB = this.openOrCreateDatabase("db_hw3", MODE_PRIVATE, null);
myDB.execSQL("DROP TABLE IF EXISTS db_entry" );
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, mb_text TEXT NOT NULL,ph_location TEXT);");

}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}


}



}


------------------

This is the Code for an Activity that contain edit text and Insert Button....I think something wrong I am doing in the try block of this code..... Please help

package com.example.hw3;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class A1 extends Activity {
private Button b3;
private EditText text1;
private EditText text3;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);

initControls1();
}

protected void initControls1()
{
b3 = (Button)findViewById(R.id.SendButton);
text1 = (EditText) findViewById(R.id.et1);
text3 = (EditText) findViewById(R.id.EditText04);
b3.setOnClickListener(new Button.OnClickListener()
{ public void onClick (View v){ DoWork1(); }});



}
protected void DoWork1()
{
SQLiteDatabase myDB= null;
// SQLiteDatabase myDB;
//String TableName = "db_entry";
String str= text1.getText().toString();
text3.setText(str);
//String Data="";
try {
myDB = this.openOrCreateDatabase("db_hw3", MODE_PRIVATE, null);

/* Insert data to a Table*/
myDB.execSQL("INSERT INTO db_entry"
+ " (mb_text, ph_location)"
+ " VALUES (str,pp );");
}
catch(Exception e) {
Log.e("Error", "Error", e);
}




}

}