How to import a .csv file to SQLite for Android? - android

I have never used a database before, so bear with me. I am programming for the Android tablet and don't have one in my possession yet, but will be getting it within a week. I have a .csv file in Excel whose data I would like to use in my app. I am aware that Androids come with SQLite, but exactly what that is or how to use it is beyond me.
I'm going to work on learning about databases as I go, but first-- How do I get my csv file into SQLite? Can I do that from my PC or do I have to wait and do it on the tablet? What do I need to download to make this happen, what exactly and where do I need to type, etc... I need really basic instructions on doing this and everything I've found online has gone way over my head due to my complete lack of experience.

You can download SQLite to you PC and tinker with it in the meantime. The import doesn't have to happen on the phone.
You can find many ways to do this process in their wiki.

Try something like this:
...
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(",");
if (colums.length != 4) {
Log.d("CSVParser", "Skipping Bad CSV Row");
continue;
}
ContentValues cv = new ContentValues(3);
cv.put(dbCol0, colums[0].trim());
cv.put(dbCol1, colums[1].trim());
cv.put(dbCol2, colums[2].trim());
cv.put(dbCol4, colums[3].trim());
db.insert(TABLE, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
How to read the CSV file
Copy file.csv into the assets folder, and read it like this:
String mCSVfile = "file.csv";
AssetManager manager = context.getAssets();
InputStream inStream = null;
try {
inStream = manager.open(mCSVfile);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
...

Related

Reading a large text file with over 130000 line of text

How can i read a large text file into my Application?
This is my code but it does not work. My code must read a file called list.txt. The code worked only with a file with only 10.000 lines.
can someone helps me?
Thanks!
My code:(Worked with small files, but not with large files)
private void largefile(){
String strLine2="";
wwwdf2 = new StringBuffer();
InputStream fis2 = this.getResources().openRawResource(R.raw.list);
BufferedReader br2 = new BufferedReader(new InputStreamReader(fis2));
if(fis2 != null) {
try {
LineNumberReader lnr = new LineNumberReader(br2);
String linenumber = String.valueOf(lnr);
while ((strLine2 = br2.readLine()) != null) {
wwwdf2.append(strLine2 + "\n");
}
// Toast.makeText(getApplicationContext(), linenumber, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), wwwdf2, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Since you are processing a large file, you should process the data in chunks . Here your file reading is fine but then you keep adding all rows in string buffer and finally passing to Toast.makeText(). It creates a big foot-print in memory. Instead you can read 100-100 lines and call Toast.makeText() to process in chunks. One more thing, use string builder instead of string buffer go avoid unwanted overhead of synchronization. You initializing wwwdf2 variable inside the method but looks it is a instance variable which I think is not required. Declare it inside method to make it's scope shorter.

Connect to third-party app's database?

I am creating an app for personal use only, and for a while now I have been trying to figure out how to connect to a third-party's app's database without sharing content user ID. Note: I have root, so all root options are welcome too!
I have been using this:
protected String RefreshMessages() {
try {
String line;
String result = "";
Process process = Runtime.getRuntime().exec("su");
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
stdin.write("su -c 'sqlite3 \"/data/data/<app>/databases/database.db\" \"" + REFRESH_QUERY + "\"'\n".getBytes());
stdin.write("exit\n".getBytes());
stdin.flush();
stdin.close();
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while((line = br.readLine()) != null){
result = result + line;
}
Log.d("[Output]", result);
br.close();
process.waitFor();
process.destroy();
return result;
} catch (Exception ex) {
Log.d("ERR-RFRSH-MSG", ex.toString());
return "0";
}
}
And spamming it in a service as fast as I can to get updates, but seriously.. there has to be a better option available... especially as a root user. Something that allows me to connect properly and get live information from the database. I have literally Google'd for months and this is the best I could find.
I also found stuff about copying the database into a TEMP folder within my app's domain and then reading contents, but I need to read information quick. Constantly copying over the whole database is too slow.
Please tell me I have better options, Android wizards!

How to Read and Write to a csv file in Android?

I want to store 8 integers into a .csv file(the filename will be taken as an input from a EditText) and retrieve them when I want to.
To get the filename you can use this:
EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();
Then write the file on disk:
try {
String content = "Separe here integers by semi-colon";
File file = new File(fileName +".csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
To read the file:
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName+".csv"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Then to have the Integers you can use split function:
String[] intArray = sCurrentLine.split(";");
Opencsv doesn't work on Android due to JDK issue.
If you see simular to the following:
05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of:
Ljava/beans/Introspector;
It is because Android only ported over a subset of java.beans into its
version of java. You can see a list of what is support at
https://developer.android.com/reference/java/beans/package-summary
There are plans to switch over to reflection in opencsv 5.0 but I
doubt it will remove all our dependencies on java.beans. For now the
best suggestion for opencsv is to steer clear of the com.opencsv.bean
classes until Android fully supports java.beans or we are successful
in removing java.beans.
Another possibility is to try another csv library. I checked apache
commons csv and super-csv and apache does not convert to beans but
super-csv does using only reflection.
Source: https://sourceforge.net/p/opencsv/wiki/FAQ/#getting-noclassdeffounderror-when-using-android
CSV is normal text file, where values are divided by character ";" so, you can write it using BufferedWriter for example.
BufferedWriter

How can i insert data in an sqlite table from a text file in android

In my application I am creating many tables in the sqlite database but in that one table contains more than 100k rows I get data from server using http connection. Getting this kind of many records causing much more time for me so to avoid this problem I want to connect my tablet through PC and I want to place txt file(which has insert statements) in the tablet.
Now how can I access that txt file in my application and how can I insert data in my table using that txt file and into which location I have to copy this txt file to access it from my application.
My DB file size also 273 MB, so I don't think it's possible to place it in the assets folder
you can copy the file to assets folder
code example:
private void initDatabaseData(SQLiteDatabase db) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(context.getAssets().open(file_name)), 1024 * 4);
String line = null;
db.beginTransaction();
while ((line = br.readLine()) != null) {
db.execSQL(line);
}
db.setTransactionSuccessful();
} catch (IOException e) {
Log.e(LOG_TAG, "read database init file error");
} finally {
db.endTransaction();
if (br != null) {
try {
br.close();
} catch (IOException e) {
Log.e(LOG_TAG, "buffer reader close error");
}
}
}
}
above code require the every line of the txt file is a sql sentence

How to parse the CSV file in android application?

I have a CSV file in drawable/asset folder. In the CSV file there are four columns. First one is for date and rest three are for integer data.
I need to parse this CSV file and save the data in separate arrays.
I have searched for a solution, but I don't get proper idea on how to do this.
I like this csv reader: https://mvnrepository.com/artifact/net.sf.opencsv/opencsv/2.3
Just add it to your project.
Example code (assuming there is the file assets/test.csv):
String next[] = {};
List<String[]> list = new ArrayList<String[]>();
try {
CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("test.csv")));
while(true) {
next = reader.readNext();
if(next != null) {
list.add(next);
} else {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
You can access the imported data with, for example,
list.get(1)[1]
That would return a string.

Categories

Resources