External Database access in Android - android

Can I talk to the database of another application from inside my code?

Only, If that database is not a private to that application. (Also if your device is rooted then you can access any application's database) Also if that database is like a content Provider then you can access other application's database in your application. Like Android native phonebook database. (As it used their database as a content Provider.)

No, You can not.
same question
But you can try it by rooting the phone
link >> link
Reading DB from SDCARD>>
see the location of DB file then create a method inside a class, the method is like:
public class DB_Path {
public final SQLiteDatabase getDB() {
File dbfile = new File("path of file like : /sdcard/TheDataBaseFile");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}
}
initialize like
public DB_Path dbp = new DB_Path();
public SQLiteDatabase db = dbp.getDB();
after that you can call the cursor with db.
Cursor cur = db.rawQuery("the sql query",null);
Reference link >> link
How to get table names from database >> link

Related

connect to sqlite in android using connection string

I am so new and new in android , i have a big problem with it, i create an app that needs to connect to database .so i create a database using sqllite expert pro as you can see here :
CREATE TABLE [user](
[name] tEXT,
[id] INT PRIMARY KEY);
My database name is a .i want to read the value from the user table as you can see here in my code :
SQLiteDatabase mydatabase = openOrCreateDatabase("a",MODE_PRIVATE,null);
Cursor resultSet = mydatabase.rawQuery("Select * from user",null);
resultSet.moveToFirst();
String username = resultSet.getString(1);
String password = resultSet.getString(2);
TextView tv = (TextView) findViewById(R.id.editText1);
tv.setText("Text is: " + username + password);
but it doesn't work ,should i add connection string to my code ,or should i import the database into my project,because the database is in my workspace folder.
I use this code to create a test database inside android ,but it doesn't work again?
SQLiteDatabase mydatabase = openOrCreateDatabase("ali",MODE_PRIVATE,null);
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('admin','admin');");
Cursor resultSet = mydatabase.rawQuery("Select * from TutorialsPoint",null);
resultSet.moveToFirst();
String username = resultSet.getString(1);
String password = resultSet.getString(2);
Your database needs to be copied to the phone's internal storage first. You can do it manually or with the help of this library Android SqliteAsset Helper
Follow the right method for creating database in android using codes:
Create a class that extends SqliteOpenHelper.
public class DbOpener extends SqliteOpenHelper {
DbOpener(Context c){
super(c, 1, "a", null, 1); //where "a" is the database name
}
public void onCreate (SqliteDatabase db){
db.execSQL("CREATE TABLE TutorialsPoint (Username VARCHAR, Password VARCHAR);");
}
}
Then in your activity use it as follows:
DbOpener opener = new DbOpener(this);
SqliteDatabase myDatabase = opener.getWritableDatabase();
//Now you can perform all your queries (including insertions) using the myDatabase object
First, you should change your database name to a readable one like "mydata.db"
Second, there is no need for connection string on using SQLite in Android like the usual ways we do with accessing database from Java code.
You need to access database by using SQLiteOpenHelper. Tutorial from Android SQLite database and content provider. Try to get the feel with Android and SQLite from the tutorial.
Then, after you've mastering the concept and know the how, you can use your predefined database by utilizing Android SQLiteAssetHelper

Query SQLite Database on External Storage with Android App

I need to write an Android app that can query a SQLite database that is stored on a device's external storage, but I cannot figure out how despite everything I have looked at online, most of which was on StackOverflow. Here's the constructor for the class that I want to use to work with the database. I believe that this piece of code is sufficient information for my question, but if it is not, I can provide more.
public KetaiSQLite(PApplet p, String dbName, int dbVersion, String dataRootDir)
{
this.context = p.getActivity().getApplicationContext();
DATABASE_NAME = dbName;
DATABASE_VERSION = dbVersion;
DATA_ROOT_DIRECTORY = dataRootDir;
OpenHelper openHelper = new OpenHelper(this.context, dbName);
this.db = openHelper.getWritableDatabase();
}
How can I modify this so that I can query from a database on external storage? At the moment, db.getPath() returns /data/data/processing.test.app/databases/appData.sqlite.
SQLiteOpenHelper handles database creation and version management.
But you don't want that, so just use the openDatabase() function directly.

android:copy sqlite database to device manually

I have developed an android app which will run just on one device,and i have an SQLite DB and i want to copy that in //data/data//databases directory on device internal storage,but i have two problems:
This directory for my app is not visible for me, while files of some other apps are visible.
I created this directory manually and copied the DB there, but it didn't work.
In emulator i copied db to the files that was created automatically and app works properly.
thanks for your helps
and this is my sqlite code:
SQLiteDatabase db=openOrCreateDatabase("a",MODE_PRIVATE, null);
Cursor c=db.rawQuery("SELECT * FROM person WHERE id=?", new String [] {String.valueOf(1)});
c.moveToFirst();
String name=c.getString(c.getColumnIndex("name"));
db.close();
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(name);
You have to copy your SQLite database in assets folder, which is inside the main project folder. You cannot place it directly inside the /data/data/your application package/.
Follow this tutorial to get going on this.
There is a line in code,
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
replace this with
private static String DB_PATH;
and
update the constructor to
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
// you will get a warning if you'll try to hardcode your path
DB_PATH = myContext.getDatabasePath(DB_NAME).getPath();
}
rest is all explained in this tutorial.

Cant find database when app starts

Im trying to do a simple example using my own SQLite database, android and greenDAO generator to generate classes for my android app.
My database file is defined this way:
1) Create a database called "OneTableDB" (without extension, SQLite 3) with the following structure:
Entity: Professor
professorID: primarykey
name: text
age: int
Entity: android_metadata
locale: text
Then i populated android_metadata with the value 'en_US', and the entity with few rows.
2) placed on my Android app structure inside: proj_root/databases/
Full path to database file: proj_root/databases/OneTableDB
3)i have a method to check whether database exists or not (in my case, it has to exist, since i placed inside databases folder)
private boolean databaseExists() {
SQLiteDatabase sqliteDatabase = null;
try {
String databasePath = DB_PATH + DB_NAME;
File f = new File(DB_PATH + DB_NAME);
f.exists();
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (sqliteDatabase != null) {
sqliteDatabase.close();
}
return sqliteDatabase != null ? true : false;
}
//DB_PATH = /data/data/com.myapp.android_dao_tests/databases/
//DB_NAME = OneTableDB
debugging on f.exists(), it returns false value and then breaks on
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
During the debugging i used adb shell to check if the path was right, and in fact i can navigate to /data/data/com.myapp.android_dao_tests/ and there is no databases folder!
Any idea how can i solve this problem?
Thanks in advance?
The DB "template" is saved in the assets/ folder, in order for it to be bundled in the apk. The code then copies the DB from assets/ to databases/ to make it accessible as a regular SQLite DB.
After further investigation, it seems like Android refuses to acknowledge the new DB as its own. Apparently, the built-in DB mechanism wasn't meant to be used this way.
The correct way to approach it is by keeping the data in textual format in assets/ so that if the app starts and finds there's no DB, it will create the schema itself, and populate it with the data in the text files from the assets/ folder.

Create database from prestored db file on SDcard

The database i want to import is over 5Mb. When i put the .db in asset folder i got some errors. So i split the database into smaller db files and assemble the pieces to create the database. So is there any way i can import the database from sdcard and start using the db without splitting and assembling?
Thanks in advance.
Yes, there is:
place the database file inside the sdcard then create a method inside a class, the method is like:
public class DB_Path {
public final SQLiteDatabase getOrders() {
File dbfile = new File("/sdcard/TheDataBaseFile");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}
}
-----------------------
initialize like
public DB_Path dbp = new DB_Path();
public SQLiteDatabase db = dbp.getOrders();
after that you can call the cursor with db.
Cursor cur = db.rawQuery("the sql query",null);

Categories

Resources