Creando múltiples bases de datos en Android

I am trying to create multiple database tables in android where each table will represent an account. I am struggling to get more then one table per database

Would anyone have any sample code they could show me? Or any suggestions?

Muchas Gracias

preguntado el 09 de marzo de 12 a las 23:03

4 Respuestas

I don't know anything about your app, but the way you're designing your database schema sounds a little questionable. Does creating a table for every account (whatever an "account" might be) really make the most sense?

Regardless, creating tables in SQLite is pretty straightforward. In your SQLiteOpenHelper class, you can call db.execSQL(CREATE_TABLE_STATEMENT) to create a table, where CREATE_TABLE_STATEMENT is your SQL statement for a particular table. You can call this for every table you need created. This is typically going to be called in your SqliteOpenHelper's onCreate method when the database is initialized, but you can call it outside of the helper as well.

respondido 10 mar '12, 00:03

See I am not too sure how to format the database. I was thinking each account have its own table with all the transactions listed on it which seems to be the easiest. - user1235720

Again, I'm not sure what an account is exactly in your case, but a table-per-account design is not a good choice. What happens when you have 100,000 accounts? Do you have 100,000 tables? Why not have an accounts table and a transactions table. The transactions table then references the accounts table using a foreign key. - trato de tyler

there would only be 2-3 accounts with anywhere from 1 to 10,000 transactions on each so would be the easiest way. - user1235720

If you are going to use a fair amount of tables and data, including a prepopulated database in your assets folder is another way to go.

respondido 10 mar '12, 00:03

When I started to use databases on android I found this very helful.

http://developer.android.com/resources/tutorials/notepad/index.html

ps now that you mentioned that there are only 2-3 accounts, creating one table/account sounds much more reasonable than first expected.

But it really depends on what you are planning to do with the data and where your performance requirements are. One could even use one single table or as well multiple tables for each (fixed) type of transaction - if the data for transaction types have very different structure.

respondido 10 mar '12, 00:03

Creating database table in Android is pretty straghtforward: db.execSQL(CREATE_TABLE_STATEMENT); where db is SQLiteDatabase object and CREATE_TABLE_STATEMENT is your create table sql statement

As in your question you did not explain clearly the requirement of your app, but I can see a few cons in your approach of creating one table for each user

  1. If there are many users created, u will have many tables in ur database
  2. If later on you have some information, settings that would be shared across some users or all user, you will have to replicate them in every user single table.

My recommendation would be you have one table for users, and another table for transactions with one column is user_id to link back to the user table. It would be easier to expand or maintain your database later.

Espero eso ayude :)

respondido 10 mar '12, 03:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.