dimanche 27 septembre 2015

Unable to read contents of sqlite database

I have an sqlite database where I have two columns( id and Url). I want to get the contents of url from a listview method. The sqlite class is as below.

public class SqliteController extends SQLiteOpenHelper {
private static final String LOGCAT = null;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "storeingurl";

//  table name
private static final String TABLE_GREETINGS = "urllist";

// Greetings Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_URL = "url";

public SqliteController(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.d(LOGCAT, "Created");
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_GREETINGS_TABLE = "CREATE TABLE " + TABLE_GREETINGS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_URL + " TEXT"
             + ")";
    db.execSQL(CREATE_GREETINGS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_GREETINGS);

    // Create tables again
    onCreate(db);
}
/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new greetings
void addGreetings(SqliteClass sqclass) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_URL, sqclass.getUrl());
    // Inserting Row
    db.insert(TABLE_GREETINGS, null, values);
    db.close(); // Closing database connection
}
// Getting single greeting
public SqliteClass getGreeting(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_GREETINGS, new String[] { KEY_ID,
                    KEY_URL }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    assert cursor != null;
    SqliteClass sq = new SqliteClass(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1));
    // return contact
    return sq;
}
// Getting All Greetings
public List<SqliteClass> getAllGreetings() {
    List<SqliteClass> greetingList = new ArrayList<SqliteClass>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_GREETINGS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            SqliteClass sql = new SqliteClass();
            sql.setID(Integer.parseInt(cursor.getString(0)));
            sql.setUrl(cursor.getString(1));
            // Adding contact to list
            greetingList.add(sql);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    // return greeting list
    return greetingList;
}
// Updating single greeting
public int updateGreetings(SqliteClass sqliteCls) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_URL, sqliteCls.getUrl());

    // updating row
    return db.update(TABLE_GREETINGS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(sqliteCls.getID()) });
}
// Deleting single greeting
public void deleteGreetings(SqliteClass sql) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_GREETINGS, KEY_ID + " = ?",
            new String[] { String.valueOf(sql.getID()) });
    db.close();
}
// Getting greetings Count
public int getGreetingsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_GREETINGS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int a = cursor.getCount();
    cursor.close();
    db.close();
    // return count
    return a;
}
} 

I am using the following method to read the contents of the url column from the listview method. But, I am unable to read the contents of Url. Can anyone help me figure out the correct way to read the content of url.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent in = new Intent(SaveUrlActivity.this, DisPlayWebPageActivity.class);
            in.putExtra("page_url", db.getGreeting(position).toString());
            startActivity(in);
        }
    });

Aucun commentaire:

Enregistrer un commentaire