Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MessagesDbHelper extends SQLiteOpenHelper {
- private String[] allColumns = {"id", "timestamp", "author", "message"};
- public MessagesDbHelper(Context ctx) {
- super(ctx, "messages.db", null, 5);
- }
- @Override
- public void onCreate(SQLiteDatabase sqLiteDatabase) {
- sqLiteDatabase.execSQL("create table messages (id string primary key, timestamp long not null, author text not null, message text not null );");
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
- sqLiteDatabase.execSQL("drop table if exists messages;");
- onCreate(sqLiteDatabase);
- }
- public void test() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- XmppContact contact = new XmppContact("ooooooooo@w.tu", "999");
- long delay = System.currentTimeMillis();
- HistoryStorage history = HistoryStorage.getHistory(contact);
- history.clearAll(true);
- for (int i = 0; i < 100; ++i) {
- history.addText("sthsthxghhhhhhhhsttttttttttttttwwytrujkfk", true, "ii" + i, 9869905);
- }
- Log.e("HistoryStorage time", ""+(System.currentTimeMillis() - delay)/1000);
- long delay2 = System.currentTimeMillis();
- MessagesDbHelper messagesHelper = new MessagesDbHelper(SawimApplication.getContext());
- SQLiteDatabase db = messagesHelper.getWritableDatabase();
- db.execSQL("drop table if exists messages;");
- messagesHelper.onCreate(db);
- for (int i = 0; i < 100; ++i) {
- ContentValues values = new ContentValues();
- values.put("id", "ooooooooo@w.tu" + i);
- values.put("timestamp", 9869905);
- values.put("author", "ii");
- values.put("message", "sthsthxghhhhhhhhsttttttttttttttwwytrujkfk");
- db.insert("messages", null, values);
- }
- Cursor rowIds = messagesHelper.getIds(db);
- for (int i = 0; i < 100; ++i) {
- rowIds.moveToPosition(i);
- String rowId = rowIds.getString(rowIds.getColumnIndex("id"));
- Cursor cursor = messagesHelper.getRowById(db, rowId);
- cursor.moveToFirst();
- Log.e("get", cursor.getString(cursor.getColumnIndex("author"))+" "+
- cursor.getString(cursor.getColumnIndex("message"))+" "+new Date(cursor.getLong(cursor.getColumnIndex("timestamp"))).toLocaleString());
- }
- Log.e("Database time", ""+(System.currentTimeMillis() - delay2)/1000);
- }
- }).start();
- }
- public Cursor getIds(SQLiteDatabase db) {
- return db.rawQuery("select id from messages limit 1000", new String[]{});
- }
- public Cursor getRowById(SQLiteDatabase db, String id) {
- return db.query("messages", allColumns, "id='" + id + "'", null, null, null, null, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement