mardi 30 décembre 2014

Errors in my SQLite Database for Android

I am having multiple issues with my SQLite Database with Android. I am new to developing and learning on my own. SO please use laymens terms and assume i do not know what you are talking about lol. Specifically i am having trouble wit the methods:



  • getLast()

  • deleteLast()

  • getLatestSubjectForContact()

  • getAllRows()

  • isEmpty()


If you see any other errors i will accept feedback. Thanks for all help you can provide. here is my database code



package com.swavey.testing;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.security.KeyChain;

import java.util.Date;

/**
* Created by Adrian on 11/5/2014.
*/
public class smsDatabase {

private static final String KEY_ID = "_id";
private static final int COLUMN_ID =0;

// database info
public static final String DATABASE_NAME = "texts";
public static final String DATABASE_TABLE = "mainTable";
public static final int DATABASE_VERSION = 4;

// list of fields
public static final String KEY_ADDRESS = "address";
public static final String KEY_BODY = "body";
private static final String KEY_DATE = "date";
private static final String KEY_READ = "read";
private static final String KEY_THREADID = "thread_id";
private static final String KEY_TYPE = "type";
private static final String KEY_SEEN = "seen";

//list of field numbers
private static final int COL_ADDRESS = 1;
private static final int COL_BODY = 2;
private static final int COL_DATE = 3;
private static final int COL_READ = 4;
private static final int COL_THREADID = 5;
private static final int COL_TYPE = 6;
private static final int COL_SEEN = 7;

//create string array of all fields;
public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADDRESS, KEY_BODY, KEY_DATE,
KEY_READ, KEY_THREADID, KEY_TYPE, KEY_SEEN};


private static final String DATABASE_CREATE_SQL = "create table " + DATABASE_TABLE
+ " (" + KEY_ID + " integer primary key autoincrement, "
+KEY_ADDRESS + " text not null, "
+KEY_BODY + " text not null, "
+KEY_DATE + " text not null, "
+KEY_READ+ " text not null, "
+KEY_THREADID+ " text not null, "
+KEY_TYPE+ " text not null, "
+KEY_SEEN+ " text not null"
+ ");";

private final Context context;

private DatabaseHelper dbHelper;
private SQLiteDatabase db;

public smsDatabase (Context cxt) {
this.context = cxt;
dbHelper = new DatabaseHelper(context);
}

//open database
public void open() {
db = dbHelper.getWritableDatabase();
}
//close database
public void close() {
dbHelper.close();
}

//returns a cursor with all rows loaded
public Cursor getAllRows() {
String where = null;
Cursor cursor = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null,
null, null);

if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}

public boolean isEmpty() {
String where = null;
Cursor cursor = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null,
null, null);
if (cursor!=null) return false;
return true;
}


// insert sms into table
public long insertSMS (SMS sms) {
ContentValues iv = new ContentValues();
iv.put(KEY_ADDRESS, sms.getAddress());
iv.put(KEY_BODY, sms.getBody());
iv.put(KEY_DATE, sms.getDate());
iv.put(KEY_READ, sms.getRead());
iv.put(KEY_THREADID, sms.getThread_id());
iv.put(KEY_TYPE, sms.getType());
iv.put(KEY_SEEN, sms.getSeen());




return db.insert(DATABASE_TABLE, null, iv);
}

public Cursor getLast() {
SMS txt = new SMS();
String where = null;
Cursor c = db.query(true, DATABASE_TABLE,ALL_KEYS, where, null, null, null, null, null);
c.moveToLast();
return c;

}

public void deleteLast() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE,ALL_KEYS, where, null, null, null, null, null);
String las = Integer.toString(c.getCount());
db.delete(DATABASE_TABLE, KEY_ID + "=" + las, null);
}

public void deleteRow(String address, String date, String body ) {
db.delete(DATABASE_TABLE, KEY_ADDRESS + "=" + address +" and " + KEY_DATE + "=" +
date + " and " + KEY_BODY + "=" + body, null);
}

public String getLatestSubjectForContact() {
Cursor c = getLast();
String sub = c.getString(c.getColumnIndex("body"));
if (sub.length() > 30) {
sub = sub.substring(0, 30) + "...";
return sub;
}
sub = sub + "...";
return sub;
}

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}


public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
_db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE);
onCreate(_db);
}
}
}

Aucun commentaire:

Enregistrer un commentaire