Copy Data from CSV file to Sqlite file in Android - android

I want to copy the data from CSV file to SQLite file for my android application.SQLite table structure i.e. the columns are different than the CSV file columns. There are more columns in the CSV file. I am trying to use DB Browser For SQLite software.
Is it possible to copy data through these this Application or should I be writing some code to do that? Can anyone please suggest?

I would like to suggest using SQLite Studio for importing CSV data and create an SQLite database file from that CSV file. You can find the details here. You can also do that from your command line argument as shown in the link provided.
$ sqlite3 mydb // This creates a new database
.mode csv // Enable to import from the csv
.import c:/sqlite/city.csv cities // import a csv file and create a table having the column name same as found in the first row of your csv file
Once you have got your SQLite database, you can now add, drop or modify columns and their names using the database queries.
Once you have prepared the database, you can put this database file in the asset directory of your Android Studio project and while launching the application, copy the database from your asset directory into your local storage so that it can be accessed by your Android application. I am attaching a sample code for copying database file from the asset directory to your internal storage.
private void copyFromAssetsAndCreateDatabase() {
InputStream mInputStream;
private String DB_PATH = "/data/data/" + "your.application.package.goes.here" + "/databases/";
try {
mInputStream = getApplicationContext().getAssets().open(YOUR_DATABASE_NAME);
File dir = new File(DataHelper.DB_PATH);
if (!dir.exists()) dir.mkdir();
File f = new File(DataHelper.DB_PATH + YOUR_DATABASE_NAME);
if (!f.exists()) {
f.createNewFile();
}
OutputStream mOutput = new FileOutputStream(f);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInputStream.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Hope that helps!

The main way is Read File like this:
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
Get title of the columns:
String dataA = br.readLine();
String[] eachLineA = dataA.split(",");
Read line by line :
while ((dataA = br.readLine()) != null)
Split and Remove " from each cell,
fitData.add(eachLineA[i].replaceAll("\"", ""));
Finally add to database
myDb.addHandler(fitData.get(0), fitData.get(1), fitData.get(2), fitData.get(3), fitData.get(4));
This is a while loop:
while ((dataA = br.readLine()) != null) {
while (way && dataA != null) {
eachLineA = dataA.split(",");//
if (eachLineA.length < leA && fitData.size() < leA) {
way = true;
dataA = br.readLine();
} else way = false;
for (int i = 1; i <= eachLineA.length - 1; i++) {
fitData.add(eachLineA[i].replaceAll("\"", ""));
Log.i("Tag", "N:" + i);
}
}
myDb.addHandler(
fitData.get(0), fitData.get(1), fitData.get(2), fitData.get(3), fitData.get(4)
);
fitData.clear();
way = true;
}

If the columns in your CSV file don't map into the columns in your SQLite table then you need to do some data transformation first i.e. reduce the number of columns to the number of columns in your table, and name them accordingly.
Then you can use the built-in CSV importer to import the data.
Open a terminal and launch a sqlite prompt.
Set mode to csv
sqlite> .mode csv
Import the data from your csv file with the following command:
sqlite> .import c:/path/to/your/file/use/forward/slashes destination_table_name
This is a really good tutorial. http://www.sqlitetutorial.net/sqlite-import-csv/

Related

Android external database in assets folder

I have an android application that is supposed to read and expand a database that is already created on sqlite...it works fine on emulator by putting database in "data/data/(packagename)/database" folder on the file explorer of emulator. Now problem is occuring with the real device. Obviously it doesnt have the database to open.I tried to put database in assets folder but I am not getting to open it with the openhelper.
you should copy the .db file from your assets folder to an internal/external storage. You can use following codes,
private static String DB_PATH = "/data/data/your package/database/";
private static String DB_NAME ="final.db";// Database name
To create a database,
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
Check that the database exists here: /data/data/your package/database/DB Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = getApplicationContext().getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
i hope it should help you.
you cant access the database from asset folder directly you need to copy it first to the path data/data/(packagename)/database then using it :
private String DB_PATH = "/data/data/" + "yourpackaename" + "/databases/" + "db.db";
in your onCreate()
is = getAssets().open("db.db");
write(is);
Then the method to call:
public void write(InputStream is) {
try {
OutputStream out = new FileOutputStream(new File(DB_PATH));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
is.close();
out.flush();
out.close();
System.err.println(out + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
You need to first copy the Database file from assests to application data location using java code.Can You Post some code to show How are you opening or handling the database?
You cannot directly open files from assets folder. Instead, you need to copy the contents of your assets folder on an internal/external storage and later use the File path to open the file.
In emulators, its easier for you to access the data folder of your apps. However, on a real non-rooted android device, its not possible due to security reasons.
Do you have a pre-populated database and looking to integrate into your app? If yes, you can simply do with my library
On your app's first launch after installation
SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM);
On subsequent launches
SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE);
Simply fire SQL queries
database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');");
Get results on Array in CSV, JSON, XML
ArrayList<String> rows=new ArrayList<String>();
rows=database.sqlEjectCSV("SELECT * FROM food;");
for (int i=0;i<rows.size();i++)
{
//Do stuffs with each row
}
You need to include my library for this. Documentations here:
https://github.com/sangeethnandakumar/TestTube

Android work with db file from SD card

I wrote an app which logged data and saved it via SQL into a .db File. I had a method copying it from internal memory to SD card.
Now i wrote a second app, which needs to work with this particular .db file. As i think, that apps can't get access to package files from other apps
(in this case
/data/data/app1_package/databases/my_database.db
)
i need somehow to work with my DB on the SD Card. How do i do that?
Can i use this path in my SQLiteHelper class? Should i copy it from SD to my package, is that even possible (access rights etc.)?
I'm a beginner in databases, some help would be nice.
You can open any readable file path as a database:
File dbFile = new File( Environment.getExternalStorageDirectory(), "myfile.db" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,null,null);
Note: check if sd-card is mounted before using this code.
yes place the DB file in your assets folder and get it this way :
DB_PATH="/data/data/app1_package/databases/my_database.db"
in your create :
is = getAssets().open("Meaniningss.db");
write(is);
the method :
public void write(InputStream is) {
try {
OutputStream out = new FileOutputStream(new File(DB_PATH));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
is.close();
out.flush();
out.close();
System.err.println(out + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}

[Android SDK]Can't copy external database (13MB) from Assets

I need a list of italian words for a game I'm developing but I can't actually make it copy my database from assets. I tried quitea lot of solutions I found on the website, such as:
Using your own SQLite database in Android applications
how to copy large database which occupies much memory from assets folder to my application?
Load files bigger than 1M from assets folder
But I had no luck, it keeps on giving me this error on the line
os.write(buffer, 0, len);
but I can't understand why. Here's the function's code and the constants I'm using.
A strange thins is that my database stops copying after 11.45MB, just 1 MB away from the goal.
Can someone help me solve this? Many thanks :)
Use SQLiteAssetHelper, which has a debugged version of the package-the-database-with-the-app logic, so you do not need to mess with any of this yourself.
First of all by default asset folder supports max size for db file is 1mb.
You need to divide your database into parts.
Download HJSplit and divide your database into small parts
like 13MB = 13 parts each of 1MB.
demoDB.sqlitedb= 13MB
then
demodb..sqlitedb.001
demodb..sqlitedb.002
demodb..sqlitedb.003
demodb..sqlitedb.004
...
...
demodb..sqlitedb.013
Then use the following code to merge your database.
private void copyDataBase() throws IOException {
AssetManager am = mContext.getAssets();
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
byte[] b = new byte[1024];
String[] files = am.list("");
Arrays.sort(files);
int r;
for (int i = 1; i <= 9; i++) {
InputStream is = am.open("demoDB.sqlitedb.00" + i);
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
Log.i("BABY_DATABASE_HELPER", "Copying the database (part " + i
+ " of 9)");
is.close();
}
os.close();
}
private void copyDataBase() throws IOException {
AssetManager am = getAssets();
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
byte[] b = new byte[1024];
String[] files = am.list("");
Arrays.sort(files);
int r;
for (int i = 1; i <=21; i++) {
InputStream is;
if ( i < 10){
System.out.println("coping file demoDB.sqlitedb.00"+i );
is = am.open("demoDB.sqlitedb.00" + i);
}else{
System.out.println("coping file demoDB.sqlitedb.0"+i );
is = am.open("demoDB.sqlitedb.0" + i);
}
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
is.close();
}
os.close();
}
I know this was answered but I ran into something like this while creating tests where I wanted to store a particular database with faults in it to test bad data. Problem was test database was not found at all in the assets. To even see it I had to do this:
InputStream is = mContext.createPackageContext("com.activities.tests", Context.CONTEXT_IGNORE_SECURITY).getAssets().open("mydb.db");
By ignoring the security you can see it and then take the inputstream and save it to an external directory.

When opening sqlite getting an exception android

I had created database in my android app, then inserted a statement. Everything worked, so i wanted to get my database fro MY_PACKAGE/databses/ and copy it to sd card to be reachable.
This worked, but when i trying to open with my sqlite Firefox plugin i get this error:
SQLiteManager: Error in opening file Datas.sqlite - either the file is encrypted or corrupt
Exception Name: NS_ERROR_FILE_CORRUPTED
Exception Message: Component returned failure code: 0x8052000b (NS_ERROR_FILE_CORRUPTED) [mozIStorageService.openUnsharedDatabase]
Maybe i have to open with something else or i can't open this so easily ?
I will give all the code i used:
Handling my db i used all this code:
Using your own SQLite database in Android applications
Copying it to sd card this method:
public static boolean backUpDataBase(Context context) {
final String DATABASE_NAME = "Data.sqlite";
final String DATABASE_NAME_FULL = "/data/data/package/databases/"
+ DATABASE_NAME;
boolean result = true;
// Source path in the application database folder
String appDbPath = DATABASE_NAME_FULL;
// Destination Path to the sdcard app folder
String sdFolder = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + "Datas.sqlite";
File f = new File(sdFolder);
// if (!f.exists()) {
// f.mkdir();
// }
InputStream myInput = null;
OutputStream myOutput = null;
try {
// Open your local db as the input stream
myInput = new FileInputStream(appDbPath);
// Open the empty db as the output stream
myOutput = new FileOutputStream(f);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
} catch (IOException e) {
result = false;
e.printStackTrace();
} finally {
try {
// Close the streams
if (myOutput != null) {
myOutput.flush();
myOutput.close();
}
if (myInput != null) {
myInput.close();
}
} catch (IOException e) {
}
}
return result;
}
My database looks like this:
2 tables:
CREATE TABLE "Test" ("_id" INTEGER PRIMARY KEY NOT NULL UNIQUE , "Info" TEXT)
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
And code to do all i need:
//return databse which is read and write
DataBaseHelper dataBase= Main.createOrOpenDB(mContext);
Main.backUpDataBase(mContext);
db = dataBase.myDataBase;
// Step 1: Inflate layout
setContentView(R.layout.tabs_fragment_activity);
try{
db.execSQL("INSERT INTO " +"Test" +" Values ('1','Inserted');");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
So where is wrong, as insert works fine?
It sounds like there is a problem in your code to write it to the SD card (which I'm not seeing immediately).
I wonder, why are you copying it to the SDCard? It sounds like you merely want to check the file...
If that's actually your goal, then I would recommend running the emulator and simply using the DDMS view from eclipse, navigate to the file and click the button in the upper right corner whose tool-tip says "Pull a file from the device". What you get in the emulator should be exactly what you get on your phone.
try to use SQLiteOpenHelper | Android Developers

PhoneGap + android, Pre-populating database not working.

I am creating a PhoneGap (android) project. In this I am populating a database table from reading a text file. (say my Package name is com.santu.jdictionary). But when I try to copy this 0000000000000001.db , as well as Databases.db files to /data/data/com.santu.jdictionary/app_database/file__0/ folder I get an error.
void copy(String file, String folder) throws IOException
{
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
InputStream in = getApplicationContext().getAssets().open(file);
OutputStream out = new FileOutputStream(folder+file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close(); out.close();
}
Secondly, I have been able to successfully copy this file to the above said folder thru a phoneGap plugin, but after copying also PhoneGap is unable to use this file.
When ever I try to open Database with this code, a new .db file is initialized (namely 0000000000000002.db)
db = window.openDatabase("database", "1.0", "JDictionary", 10000000);
db.transaction(populateDB,errorCB, successCB);
So Where am I making mistake.
thanking in advance.
santu ghosh
I found the soultion ultimately in ::
link
I hope everyone who commented here will read this article before trying to help others

Categories

Resources