Ahoj,
vytvoril som si databazu citatov, aj DAO k tejto databaze v DAO som vytvoril metodu addAll, po zavolanie tejto metody chcem aby sa do tejto DB vlozilo niekolko citatov (bude ich vela) chcem poradit ako to co najefektivnejsie kedze ich bude vela. Vlkadam tridu DatabazHelper a DAO. Dakujem za pomoc.
Tu sa vytvori DB.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabazaHelper extends SQLiteOpenHelper {
public static final String TABLE_CITATY = "citaty";
public static final String CITATY_ID = "_id";
public static final String AUTOR = "autor";
public static final String KATEGORIA = "kategoria";
public static final String CITAT = "citat";
public static final String NADPIS = "nadpis";
public static final int DATABASE_VERSION =1;
public static final String DATABASE_NAME = "citaty";
public DatabazaHelper (Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
private static final String DATABASE_CREATE = "create table " + TABLE_CITATY
+ "(" + CITATY_ID + " integer primary key autoincrement, " + KATEGORIA +" text not null "
+ AUTOR + " text null " + CITAT + "text not null" + NADPIS + " text null);";
public void onCreate (SQLiteDatabase database){
database.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(DatabazaHelper.class.getName(),
"Upgrading database from version "+ oldVersion + " to "
+ newVersion + ", which will destroy all data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CITATY);
onCreate(db);
}
}
A tu je DAO k danej DB
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DAO {
private SQLiteDatabase database;
private DatabazaHelper dbHelper;
public DAO (Context context) {
dbHelper = new DatabazaHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
//add citat
public void addCitat (Citat citat) {
database = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabazaHelper.CITAT, citat.getTelo());
values.put(DatabazaHelper.NADPIS, citat.getNadpis());
values.put(DatabazaHelper.KATEGORIA,citat.getKategoria());
values.put(DatabazaHelper.AUTOR,citat.getAutor());
database.insert(DatabazaHelper.TABLE_CITATY, null, values);
database.close();
}
public List<Citat> getAllCitaty(){
List<Citat> citaty = new ArrayList<Citat>();
String select = "SELECT * FROM " + DatabazaHelper.TABLE_CITATY;
database = dbHelper.getWritableDatabase();
Cursor cursor = database.rawQuery(select, null);
//throws all and ading to list
if(cursor.moveToFirst()) {
do {
Citat citat = new Citat();
citat.setID(Integer.parseInt(cursor.getString(0)));
citat.setKategoria(cursor.getString(1));
citat.setAutor(cursor.getString(2));
citat.setTelo(cursor.getString(3));
citat.setNadpis(cursor.getString(4));
citaty.add(citat);
}while (cursor.moveToNext());
}
return citaty;
}
public void AddAll(){
database = dbHelper.getWritableDatabase();
String sql = "";
database.beginTransaction();
database.execSQL("INSERT INTO " + DatabazaHelper.TABLE_CITATY + " (kategoria,autor,citat,nadpis) values (Oblubene,JA,citatat halala bala,Nadppis);");
database.setTransactionSuccessful();
database.endTransaction();
Log.w("insert citat","");
}
}
v tej metode AddAll by som chcel pridat vsetky citaty, po zapnuti aplikacii.Vlozil som len jeden aj to neviem ci je spravne.
Dakujem za rady.