This question already has answers here:
Where does Android emulator store SQLite database?
(10 answers)
Closed 7 months ago.
Hello i created a database class on andorid that below.
public class Veritabani extends SQLiteOpenHelper {
private static final String Veritabani_Adi = "Veritabanim";
private static final int Veritabani_Version = 1;
public Veritabani(Context context) {
super(context, Veritabani_Adi, null, Veritabani_Version);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ARKTABLE (id INTEGER PRIMARY KEY AUTOINCREMENT , ad TEXT, soyad TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST OperatorTablosu");
onCreate(db);
}
}
**then i declare may Veritabani class in my activity as below.**
public class DictionaryActivity extends Activity {
Spinner spnLanguage;
Veritabani objdb;;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary);
spnLanguage=(Spinner)findViewById(R.id.spnLanguage);
objdb=new Veritabani(this);
spinnerfill();
}
i don't have an exception on logcate or console.
But when i look may data/data/myappp/ i dont see database directory.
i used android 2.2 version
check In the DDMS perspective if you are using Eclipse. there you can easily find whether your database is created or not.
you want to DDBMS
Go to mnt/sdcard/yourdatabase name.it's database store in sdcard
internal memory means data/yourpackagename/yourdatabasename
Related
So I am trying to create a Database and a table in android studio. The database is created successfully but table is not creating when I am using my android phone but both the database and the table are created when I use Virtual device from the AVD.
public class Databases extends SQLiteOpenHelper {
public static final String dbname = "AttendanceDb11";
public Databases(#Nullable Context context) {
super(context, dbname, null, 2);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE Student12 ( _id INTEGER PRIMARY KEY AUTOINCREMENT, Name Text)";
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("Drop table if exists Student12");
onCreate(db);
}
}
Here is my code
It looks like that app was already installed before you created the tables. I would do one of the following:
1: Uninstalling and re-installing the app.
2: The best and better approach is to drop and recreate table in onUpdate method, and increase the db version whenever you change the schema.
I have created a database with a table.I want to add more tables dynamically to that database on the user's command.
The onOpen() method is not useful as it also changes my previous tables that already exist in the database.
I assume you are using SQLite database. If that is the case, a working solution exists here:
Create new table in existing DB in separate SQLiteOpenHelper class.
see rmkrishna's answer from that post, below:
First check the current database version for this database
private final static String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 1;
public BaseSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
and increment the database version(DATABASE_VERSION), and add your new table query in on Upgrade and oncreate method like below.
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("old query no need to change");
db.execSQL("Create your new table here");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL("Create your new table here as well this for update the old DB");
}
}
In my application i have a database which is created using DB queries in the DBHelper class. I want the onUpgrade statement to run on installation of the apk as there is a possiblity that the user might unistall and install the same application.
In this case it becomes the first installation on the device and the onUpgrade does not run which causes the application to crash.
It works perfectly if the new app is updated on the previous app.
How do i reslove this?? What is solution for such cases??
Please help !! Thanks in Advance !!
Here is my DbHelper.java
public class Dbhelper extends SQLiteOpenHelper {
public Dbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.e("","upgrade ");
database.execSQL("ALTER TABLE stock ADD db_stock_id varchar(100)");//1.1
//
}
public void dropandcreate(SQLiteDatabase database)
{
database.execSQL("DROP TABLE IF EXISTS image");
database.execSQL("DROP TABLE IF EXISTS attendance");
database.execSQL("DROP TABLE IF EXISTS stock");
onCreate(database);
}
}
Call it yourself:
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
onUpgrade(database, VERSION);
}
In my already created and deployed application, I've created a database MainDB, using a single class file which extended SQLiteOpenHelper, viz.
public class BaseSQLiteOpenHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "MainDB";
....
....
}
Issue is I've tied this class, too much to a particular functionality.
Now I'm adding a totally new module in application, which will also interact with DB, but with different new tables.
Issue is I can't use the same class, as it is conflicting in more than one way. And even if I redesign the code, it will only add complexity from functional/understanding point of view.
So, I've decided to use same DB, but different tables.
Now I've already created DB in BaseSQLiteOpenHelper class.
So, how can I create new tables in seprate class using same DB?
One approach is to use separate Database as well, Or,
Create my new table in onCreate() in BaseSQLiteOpenHelper class only (issue with this is mentioning new table in same class seems awkward, as this class has nothing to do with my new table).
Please suggest.
Thank You
First check the current database version for this database
private final static String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 1;
public BaseSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
and increment the database version(DATABASE_VERSION), and add your new table query in on Upgrade and oncreate method like below.
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("old query no need to change");
db.execSQL("Create your new table here");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL("Create your new table here as well this for update the old DB");
}
}
Done!!!
I tried to use SQLite on Android. I want to see my database file. So, I use Android File Explorer and browse this link:
Data\data\App_name But in Data\data. I cannot see my app name. (in below code will be: com/app/TimeTracker)
Here my code to test:
first is my main program:
package com.app;
public class TimeTrackerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimeTrackerDatabase database = new TimeTrackerDatabase(this);
}
}
and second is my database helper:
package com.app;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TimeTrackerDatabase extends SQLiteOpenHelper {
public TimeTrackerDatabase(Context context){
super(context, "timetracker.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(
"CREATE TABLE timerecords"+"id INTEGER PRIMARY KEY, time TEXT, notes TEXT)"
);
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
}
}
Is there any problem in above two file ? If has, please tell me what. And if not, please tell me how to fix this problem.
thanks :)
#Edit: I add my screenshot of my data folder. (No file in lib folder)
the database related issue will be solved by below updation in your code.
package com.app;
public class TimeTrackerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimeTrackerDatabase database = new TimeTrackerDatabase(this);
SQLiteDatabase db = database.getWritableDatabase(); //Add this line.
}
}
public class TimeTrackerDatabase extends SQLiteOpenHelper {
public TimeTrackerDatabase(Context context){
super(context, "timetracker.db", null, 2);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(
"CREATE TABLE timerecords" +
"(id INTEGER PRIMARY KEY, time TEXT, notes TEXT)"
);
}
Regarding your app not visible in File explorer, you need to search by package name as below
data/data/com.app and here you can see database folder where you can see all your databases created by application
actually your database is not being created due to faulty query.
write your query as:
"CREATE TABLE IF NOT EXISTS timerecords
(id INTEGER PRIMARY KEY, time TEXT, notes TEXT);"
once, the db file is created, then you can browse till the desired location
It's not Data\data\App_name , it'll be Data\data\package_name. So in your case it'll be Data\data\com.app\
Try looking your db file there.