Saving ArrayList in SQLite database in Android - java

I've been working with SQLite on android and I would like to add an arraylist to a column in a table, and then fetch the data back as an arraylist. The arraylist is a list of Longs. I've noticed that SQL has an option for storing BLOBS, however it looks like I need to convert the arraylist to a byte[] first before being able to store it as a blob in my SQLite database.
If anyone has a solution on how to save arraylists into an SQLite database that would be greatly appreciated. Or is there any other option for saving my array of data, i should consider?

To Insert :
ArrayList<String> inputArray=new ArrayList<String>();
//....Add Values to inputArray
Gson gson = new Gson();
String inputString= gson.toJson(inputArray);
System.out.println("inputString= " + inputString);
use "inputString" to save the value of ArrayList in SQLite Database
To retreive:
Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below:
outputarray is a String which is get from SQLiteDatabase for this example.
Type type = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
In SQLite use text as format to store the string Value.....

Please forgive me for savagely plagiarizing my previous answer to BLOB vs. VARCHAR for storing arrays in a MySQL table. The other answers over there are also very pertinent.
I think Con's approach is probably better than using java serialization since java's builtin serialization will need additional bytes, and non-java applications will have a harder time dealing with the data.
public static void storeInDB(ArrayList<Long> longs) throws IOException, SQLException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
for (long l : longs) {
dout.writeLong(l);
}
dout.close();
byte[] asBytes = bout.toByteArray();
PreparedStatement stmt = null; // however you get this...
stmt.setBytes(1, asBytes);
stmt.executeUpdate();
stmt.close();
}
public static ArrayList<Long> readFromDB() throws IOException, SQLException {
ArrayList<Long> longs = new ArrayList<Long>();
ResultSet rs = null; // however you get this...
while (rs.next()) {
byte[] asBytes = rs.getBytes("myLongs");
ByteArrayInputStream bin = new ByteArrayInputStream(asBytes);
DataInputStream din = new DataInputStream(bin);
for (int i = 0; i < asBytes.length/8; i++) {
longs.add(din.readLong());
}
return longs;
}
}
Note: If your lists will sometimes contain more than 31 longs (248 bytes), then you'll need to use BLOB. You cannot use BINARY() or VARBINARY() in MySQL. I realize you're asking about SQLite, but in the spirit of completely plagiarizing my previous answer, I will pretend you're asking about MySQL:
mysql> CREATE TABLE t (a VARBINARY(2400)) ;
ERROR 1074 (42000): Column length too big for column 'a' (max = 255);
use BLOB or TEXT instead

I had two ArrayList<String>, both will 1000+ entries. I looked at blobs and bytes, but for me the solution to speeding up the process and making it usable was by changing the insert method and getting rid of database.insert - Credit for this is here.
private static final String INSERT = "insert into "
+ YOUR_TABLE_NAME+ " (" + COLUMN_1 + ", "
+ COLUMN_2 + ") values (?, ?)";
public void insertArrayData(ArrayList<String> array1,
ArrayList<String> array2) {
try {
database.open();
} catch (SQLException e) {
e.printStackTrace();
}
int aSize = array1.size();
database.beginTransaction();
try {
SQLiteStatement insert = database.compileStatement(INSERT);
for (int i = 0; i < aSize; i++) {
insert.bindString(1, array1.get(i));
insert.bindString(2, array2.get(i));
insert.executeInsert();
}
database.setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
} finally {
database.endTransaction();
}
try {
database.close();
} catch (Exception e) {
e.printStackTrace();
}
}
It's easily adaptable to Longs and Integers etc and lightening quick. So thankfully I didn't have to scratch my head any longer about blobs and bytes! Hope it helps.

There is an easier way that do such thing in completely another way.
you can make an string that consists of all your array values.
for that make an StringBuilder and append the values continuously and offcource with a separator (like a simbole you which you won't use in your array values . for example virtual '|' .
in code for example :
double[] mylist = new double[]{23, 554, 55};
StringBuilder str = null;
str = new StringBuilder();
for(int i=0;i<mylist.length;i++){
str.append(mylist[i]+"|");
}
String result = str.toString();
db.open();
db.insert(result);
db.close();
when you want to fetch them and use the values. get the column from database pure it to String and with the splite() opration pure each values of array in a column of array than u can easily use it :)
lets do it in code :
String str = db.getdata();
String[] list = str.split("|");
with a simple convert you can use them as double;
double mydouble = Double.parsDouble(list[1].toString());
maybe it is not standard but it is helpfull, hope help ;)

Sounds like you want to serialize the List. Here is a tutorial/intro to the Java Serialization API.

You'll have to do it manually, go through each item in the list and change it to byte before storing it in the database
for (long l : array<long>){
//change to byte here
//Store in database here
}

ArrayList<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
String joined = TextUtils.join(",", list);
Log.i(TAG, "joined strings: " + joined);
String[] array = TextUtils.split(joined, ",");
Log.i(TAG, "joined strings: " + array[0] + array[1] + array[2]);

Related

Java Setting Array values then loop through and set object field equal to array values

I have a pretty introductory question that I hope can get answered rather easily.
Currently I am trying to write something that queries my DB, returns result set (list of account id's) and then sets those account id's into an array so that I can populate an object with the values.
My object is going to be used as input into another method I have written but I can't figure out how I go about populating my account_id field on it with the values in my array that I returned in the result set. There doesn't seem to be any "setter" methods for lack of a better term on my array of type String.
I was able to get the array to populate with acct_id's from the result set and print them out so I know that I do have information coming back.
Below is the snippet I currently have, any help/improvements I could make would be greatly appreciated!
try {
connection = DriverManager.getConnection(url, user, password);
st = connection.createStatement();
rs = st.executeQuery(sql);
List<Long> array = new ArrayList<Long>();
while (rs.next()) {
array.add((long) rs.getLong("acct_id"));
for (Integer i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
GetSummaryRequest request = new GetSummaryRequest();
request.accountKey = new AccountDTO(array[i]);
}
}
} catch (SQLException e) {
System.out.println("Connection failed.");
e.printStackTrace();
return;
}
if (rs.next()) {
System.out.println(rs.getString(1));
} else
System.out.print("Failed. Try again");
}
If my understanding is correct you may need the code below which is used to store all the account id's inside an array and you can use this to pass as a parameter to another method.
ArrayList<GetSummaryRequest> array1=new ArrayList<GetSummaryRequest>();
GetSummaryRequest request = new GetSummaryRequest();
while (rs.next())
{
request=new GetSummaryRequest();
request.accountKey=rs.getString("acct_id");
array1.add(request);
}
Now you have ArrayList of GetSummaryRequest with accountKey for each object.
if i understand correctly accountkey is integer right?
you can use Integer.parseint('your string!') class to convert the string to int.
i hope it helps
First of all , storing newline separated values in one column is not a good practice. This is against atomicity principle and will lead you to problems soon - ex., total number of accounts? how do you find it?
Once you convince yourself with this, you can use only one loop like this
List<GetSummaryRequest> summaryRequests = new ArrayList<GetSummaryRequest>();
while (rs.next()) {
String em = rs.getString("acct_id");
GetSummaryRequest request = new GetSummaryRequest();
request.accountKey = em;
summaryRequests.add(request);
}
return summaryRequests;
Probably you need something like that:
// First you get all ids
List<String> accountsIds = new ArrayList<String>();
while (rs.next()) {
accountsIds.add(rs.getString("acct_id"));
}
// Then iterate ids
for (String id : accountsIds) {
GetSummaryRequest request = new GetSummaryRequest();
request.accountKey = id;
}

Improve the search functionality

How can I improve the search functionality.? I have written some codes to search for something.The search was taking too much time. And the code snippets here,
I am pulling the data from the database using this method.,
OracleConnection connection = null;
OraclePreparedStatement ptmst = null;
OracleResultSet rs = null;
OracleCallableStatement cstmt = null;
StringBuffer strBfr = new StringBuffer();
ArrayList myList = new ArrayList();
try
{
connection = (OracleConnection) TransactionScope.getConnection();
strBfr.append("select distinct .......... ");
ptmst = (OraclePreparedStatement)connection.prepareStatement(strBfr.toString());
rs = (OracleResultSet)ptmst.executeQuery();
while (rs.next())
{
HashMap hashItems = new HashMap();
hashItems.put("first",rs.getString(1));
hashItems.put("second",rs.getString(2));
myList.add(hashItems);
}
}
catch (Exception e) {
}
finally {
try {
if (ptmst != null) {
ptmst.close();
}
} catch (Exception e) {
}
try {
if (connection != null) {
TransactionScope.releaseConnection(connection);
}
} catch (Exception e) {
}
}
return myList;
In my jsp:
ArrayList getValues = new ArrayList();
getValues = //calling Method here.
for(int i=0; i < getValues.size();i++)
{
HashMap quoteSrch=(HashMap)allPOV.get(i);
first = (String)quoteSrch.get("first");
second = (String)quoteSrch.get("second");
}
Query:
SELECT DISTINCT(mtl.segment1),
mtl.description ,
mtl.inventory_item_id ,
mtl.attribute16
FROM mtl_system_items_b mtl,
mtl_system_items_tl k
WHERE 1 =1
AND mtl.organization_id = ?
AND k.inventory_item_id = mtl.inventory_item_id
AND NVL(orderable_on_web_flag,'N')= 'Y'
AND NVL(web_status,'UNPUBLISHED') = 'PUBLISHED'
AND mtl.SEGMENT1 LIKE ? --Here is the search term
Make sure organization_id , inventory_item_id and especially SEGMENT1 is indexed in your table.
Your query is pretty standard , if that doesn't work then it seems like your DB server is responding slow which could be due to number of reasons like low space , low memory , slow disk/read etc.
You can then ask your DBA/Server admins to check that.
First you need to find out the real problem
Is it the DB query
Is it the Network (is the App and the DB located on the same machine?)
Once you have identified that it is the DB query, then it becomes more of a DB question.
How does the two tables look like?
Any index used?
How does the data look like (How many rows etc)
After you have analyzed this, you should be able to post the question differently and expect an answer. I am not a DB guy, but I am sure someone would be able to provide some pointers.
Tunning has to be done:
Check TransactionScope.getConnection(); is giving connection without any delay.
Instead of creating new HashMap hashItems = new HashMap(); you can use
while (rs.next()){
myList.add(rs.getString(1) + "delimiter" + rs.getString(2));
}
in jsp use
first = allPOV.get(i).split("delimter")[0];
second = allPOV.get(i).split("delimter")[1];
so that you can reduce memory.
If possible use limit in your query, and use index on SEGMENT1 link.

Write Java Vector To Delimited File

I am connecting to an Oracle database and querying multiple tables. My current code creates the connection and calls a PL/SQL function which contains the query. Once I have the result set, I add it to a Vector (as I am unsure the number of records each query will result in).
My problem is that I am unsure how to write a delimited file from the Vector. I imagine once I have added my result set to it, it is simply one gigantic string. I need to be able to receive each field from the query and delimit between them, as well as keep rows separate.
Here is my code:
public static void main(String[] args) throws SQLException {
// instantiate db connection
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch (Exception e) {
throw new SQLException("Oracle JDBC is not available", e);
}
// define connection string and parameters
String url = "jdbc:oracle:thin:#//host:port/sid";
Connection conn = DriverManager.getConnection(url, "USERNAME","PASSWORD");
CallableStatement stmt = conn.prepareCall("{? = CALL <functionname>(?)}");
// get result set and add to a Vector
ResultSet rs = stmt.executeQuery();
Vector<String> results = new Vector();
while ( rs.next() ){
results.add(rs.getString(1));
}
// close result set, sql statement, and connection
rs.close();
stmt.close();
conn.close();
// write Vector to output file,
// where the file name format is MMddyyyy.txt
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
String dateStr = sdf.format(cal.getTime());
FileWriter fwrite = new FileWriter(dateStr + ".txt");
BufferedWriter out = new BufferedWriter(fwrite);
for(int i = 0; i < results.size(); i++)
{
String temp = results.elementAt(i);
out.write(temp);
}
out.close();
}catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
I am just unsure how to go about getting the information from the db and writing it to a delimited file. Thanks in advance!
If you are unsure about the number of fields in each of your rows, then probably, it won't be possible. Because to fetch all the field values from database, you need to know what is the type of each fields, and the number of fields.
But, I'll post an example for when you have fixed number of fields, that you know.
Suppose you have 4 columns per row. Now to display it in tabular form, you would have to use List of List.
If you are using Vector, use Vector of List.
Here's an example for List of List: -
List<List<String>> results = new ArrayList<List<String>>();
while ( rs.next() ) {
List<String> tempList = new ArrayList<String>();
tempList.add(rs.getString(1));
tempList.add(rs.getString(2));
tempList.add(rs.getString(3));
tempList.add(rs.getString(4));
results.add(tempList);
}
Then to print it, use this loop: -
for (List<String> innerList: results) {
for (String fields: innerList) {
System.out.print(fields + "\t");
}
System.out.println();
}
You can write it in the same form to your BufferedWriter.
Use results.toString() and truncate the braces([]) from resulting string to write all values as comma separated at once in the file.
//toString() returns comma separated string values enclosed in []
String resultString = results.toString();
//truncate leading '[' and termincating ']'
resultString = resultString.substring(1, resultString.length()-1);
//if comma is not your delimiter then use String.replaceAll()
//to replace `,` with your delimiter
//write comma separated elements all at once
out.write(resultString);
So here if you have added str1, str2 in the results Vector, then resultString with have value as str1, str2, which you may write at once using your BufferedWriter out.
Also please use Generics in both the sides of initialization as:
Vector<String> results = new Vector<String>();

how to add a check in Java code for fetching only non-negative values from result-set

In my Java code I have embedded a SQL query which fetches data from a database and stores it in a result-set. I want to add a function or a piece of code which will take only non-negative data from the result-set for further processing.
Assumption: The result set can contain positive/negative/zero data values as well as characters. Also i cannot change the SQL query as its out of my scope.
try something like this, i think it will do the job
private ArrayList getNegativeNumbers(ResultSet rs, String coulumnName ) throws SQLException
{
ArrayList ret = new ArrayList();
while(rs.next()){
try {
int x = rs.getInt(coulumnName);
if(x>=0){
ret.add(new Integer(x));
}
} catch (Exception ex) {
String x = rs.getString(coulumnName);
ret.add(x);
}
}
return ret;
}
UPDATED 2. Sorry for my edits, i missread the question
while (resultSet.next()) {
if(resultSet.getInt("Column name") > 0);
Processmethod(resultSet.getInt("Column name") );
}

Java Micro Edition (J2ME) - Delete element from Record Store

I have a record store which holds a list of shopping items i am currently serialising the data like so
** (inside) ItemClass **
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeUTF(getOwner());
dataOutputStream.writeUTF(getItemName());
dataOutputStream.writeInt(getQuantity());
dataOutputStream.close();
return byteOutputStream.toByteArray();
This byte array is then written to the record store like so (using for loop for each id)
for (int i = 1; i <= shoppingListStore.getNumRecords(); i++)
{
byte[] itemRecord = shoppingListStore.getRecord(i);
newItemObject.fromByteArray(itemRecord);
userShoppingList.append(newItemObject.getItemName() + " " + newItemObject.getQuantity() + " " + newItemObject.getOwner(), null);
}
The problem comes from using the ID to treverse through the record store as this may change from deletion etc and as such i keep geting invalid id being thrown. I know i cannot use For each ItemObj : Recordstore so how do i treverse through the record store without having to worry about ids ?
Call recordStore.enumerateRecords(null, null, false)
Then use RecordEnumeration.hasNextElement() and RecordEnumeration.nextRecord()

Categories