Reputation: 303
I have problem with sqlite database. I want create database in android with existing records. I create database:
public class PrzyslowiaArabskie {
private static final String TABLE_NAME = "tabela_przyslowia";
private static final String DATABASE_NAME = "przyslowiadb";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "id";
public static final String KEY_PRZYSLOWIE = "przyslowie";
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase myDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PRZYSLOWIE + " TEXT NOT NULL);" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public PrzyslowiaArabskie(Context c) {
myContext = c;
}
public PrzyslowiaArabskie open() {
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
public void close() {
myHelper.close();
}
public long createEntry(String przyslowie) {
ContentValues cv = new ContentValues();
cv.put(KEY_PRZYSLOWIE, przyslowie);
return myDatabase.insert(TABLE_NAME, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
Cursor c = myDatabase.query(TABLE_NAME, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndexOrThrow(KEY_ID);
int iPrzyslowie = c.getColumnIndexOrThrow(KEY_PRZYSLOWIE);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " " + c.getString(iPrzyslowie) + "\n";
}
return result;
}
}
What can I do if I want that create database with existing records. How to add records together with create database?
Upvotes: 1
Views: 2252
Reputation: 1544
1)If you already have the data it will be easier to use sqlite manager of firefox to insert the data.
2)If you have data in some other forms like in "CSV" or "XML" then you can simply import that file into your table using sqlite manager of Firefox.
3)Or if you have to insert data from your app,then it is best to create a bean object send it to your database helper class and insert values into your table using any query.
As an example,
void addStatesModel(StatesModel states) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, states.getName()); // StatesModel Name
values.put(KEY_CODE, states.getCode()); // StatesModel Phone
// Inserting Row
db.insert(TABLE_STATES, null, values);
db.close(); // Closing database connection
}
In this I am passing states as abean object to the method addStatesModel.
4)Or if you have data in a separate database you can just connect the two databases and then run copy command to transfer data from one table to another.Or simply using SQLite Manager you can export table from one database to a csv file or xml file and then import that file in your table.
Upvotes: 2
Reputation: 11782
You just need to insert values in the onCreate()
method, after you've created the table:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_PRZYSLOWIE + " TEXT NOT NULL);" );
ContentValues values = new ContentValues();
values.put(KEY_PRZYSLOWIE , "My text");
db.insert(TABLE_NAME, null, values);
// ... etc...
}
As a side note, if your database is more complicated than this simple example, you might want to delegate table creation and initialization to other methods or even classes.
Upvotes: 0
Reputation: 8548
You can make a file containing all the insert statements needed to insert the data and put that file in your assets folder. After you create the database, then take each line of the file and run it through rawQuery
.
There are other ways as well, as the other answer demonstrates.
Upvotes: 2