Reputation: 26981
I have an edittext that allows the user to enter in numbers up to 10.
I want to be able to save this numbers to a SQLite database as a String list.
Does SQL allow this to be done? If so how?
Such as String [] list = {1020300303,1020303001,0102003020};
Upvotes: 1
Views: 2533
Reputation: 308823
You can do it, but you'll be breaking normalization rules.
If you persist that array as a single string (comma-delimited?), you'll have to parse it to look at individual values. Not a good idea.
A normalized schema would use a one-to-many relationship between two tables. One would be the parent, the other the child with one row per value and a primary/foreign key relationship. You'd get the values back using a JOIN.
If you're so intent on doing this, you'll have to concatenate all the Strings in the array into one, with some delimiter in between them that you're sure will never appear in any of the Strings you're combining. Once you have a single delimited String, INSERT it into a String-type column in a table in your SQLite database.
Since you insist on seeing some code, here's what it might look like in Java:
String [] list = { "1020300303", "1020303001", "0102003020" };
StringBuilder concatenatedList = new StringBuilder();
for (String s : list) {
concatenatedList.append(s).append('~'); // Any delimiter will do.
}
PreparedStatement ps = connection.prepareStatement("INSERT INTO MySQLiteTable(stringColumnName) VALUES(?)";
ps.setString(1, concatenatedList.toString());
int numRowsAffected = ps.executeUpdate();
I wouldn't write it this way - no cleanup, no error handling, no good encapsulation. But it shows where you want to go.
Upvotes: 5