2

I'm just trying to figure out how to work with android SQLite and I've followed many different tutorials. But for some reason i cannot get anything to work. Here is the code im using:

DBHandler.java

package com.example.deathtrackkevin;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHandler extends SQLiteOpenHelper  {

    // All static variables
    //Database Version
    private static final int DATABASE_VERSION = 1;

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

    //DeathTrack Table Name
    private static final String TABLE_DEATHTRACK = "DeathTrack";

    //Deathtrack Table Column Names
    private static final String COL_GAME_ID = "Game_ID";
    private static final String COL_GAME_NAME = "Game_Name";
    private static final String COL_KILLS = "kills";
    private static final String COL_DEATHS = "Deaths";
    private static final String COL_DATE = "date";
    private static final String COL_MATCH = "Match";


    public DbHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_DEATHTRACK_TABLE = 
                "CREATE TABLE" + TABLE_DEATHTRACK + "("
                + COL_GAME_ID + "INTEGER PRIMARY KEY," 
                + COL_GAME_NAME + "TEXT,"
                + COL_KILLS + "TEXT," 
                + COL_DEATHS + "TEXT," 
                + COL_DATE + "TEXT,"
                + COL_MATCH + "TEXT" + ")";
        db.execSQL(CREATE_DEATHTRACK_TABLE);
    }

    //Upgrading Database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop Older Table If Exists
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_DEATHTRACK);
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    //Adding New Deathtrack TODO Create array for deathtrack
    void addDeathtrack(DeathTrack deathtrack) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COL_GAME_ID, deathtrack.get_game_id());
        values.put(COL_GAME_NAME, deathtrack.get_game_name());
        values.put(COL_KILLS, deathtrack.get_kills());
        values.put(COL_DEATHS, deathtrack.get_Deaths());
        values.put(COL_DATE, deathtrack.get_Date());
        values.put(COL_MATCH, deathtrack.get_match());

        //Inserting Row
        db.insert(TABLE_DEATHTRACK, null, values);
        db.close();
    }
    //TODO Getting Single Deathtrack
    DeathTrack getDeathtrack(int id){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_DEATHTRACK, new String[] {COL_GAME_ID, COL_GAME_NAME, COL_KILLS, COL_DEATHS, COL_DATE, COL_MATCH}, COL_GAME_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null);
        if(cursor != null)
            cursor.moveToFirst();

        DeathTrack deathtrack = new DeathTrack(Integer.parseInt(cursor.getString(0)), 
                cursor.getString(1), 
                cursor.getString(2), 
                cursor.getString(3), 
                cursor.getString(4), 
                cursor.getString(5));

        return deathtrack;
    }

    //TODO Get all deathtrack values    
    public List<DeathTrack> getAllDeathtrack(){
        List<DeathTrack> deathtrackList = new ArrayList<DeathTrack>();
        //Select all Query
        String selectWhere = "SELECT * FROM" + TABLE_DEATHTRACK;

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

        //Looping through all rows and adding to list
        if(cursor.moveToFirst()){
            do{
                DeathTrack deathtrack = new DeathTrack();
                deathtrack.set_game_name(cursor.getString(0));
                deathtrack.set_kills(cursor.getString(1));
                deathtrack.set_Deaths(cursor.getString(2));
                deathtrack.set_Date(cursor.getString(3));
                deathtrack.set_match(cursor.getString(4));
                //Add deathtrack to list
                deathtrackList.add(deathtrack);
            } while (cursor.moveToNext());
        }
        return deathtrackList;
    }
    //TODO Delete deathtrack value by game name
    public void deleteDeathtrack(DeathTrack deathtrack){
        SQLiteDatabase db = this.getWritableDatabase(); 
        db.delete(TABLE_DEATHTRACK, COL_GAME_NAME + "= ?", new String[]{            String.valueOf(deathtrack.get_game_name())});
        db.close();
    }


}

SQLTester.java

package com.example.deathtrackkevin;

    import java.util.List;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;


    public class SQLTester extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sqltester_layout);

            DbHandler db = new DbHandler(this);

            /**
             * CRUD Operations
             * */
            // Inserting Contacts
            Log.d("Insert: ", "Inserting .."); 
            db.addDeathtrack(new DeathTrack(1, "cod2", "4", "2", "02-02-12", "1"));        
            db.addDeathtrack(new DeathTrack(2, "cod3", "4", "2", "02-02-12", "1"));
            db.addDeathtrack(new DeathTrack(3, "cod4", "4", "2", "02-02-12", "1"));
            db.addDeathtrack(new DeathTrack(4, "cod5", "4", "2", "02-02-12", "1"));

            // Reading all contacts
            Log.d("Reading: ", "Reading all contacts.."); 
            List<DeathTrack> contacts = db.getAllDeathtrack();       

            for (DeathTrack cn : contacts) {
                String log = "Game_Id: "+cn.get_game_id()+" ,Game_Name: " + cn.get_game_name() + " ,Kills: " + cn.get_kills()+ " ,Deaths: " + cn.get_Deaths()+ " ,Date: " + cn.get_Date()+ " ,Match: " + cn.get_match();
                    // Writing Contacts to log
            Log.v("dt", "name "+log);
        }
        }
    }

Question Basically all i need is someone to tell me if there is something wrong..Im not getting any logcat errors or any other type of errors so i cannot use those to figure out whats going on

Regards Joe

1 Answer 1

1

It seems like your spacebar is broken...

    String CREATE_DEATHTRACK_TABLE = 
            "CREATE TABLE" + TABLE_DEATHTRACK + "("
            + COL_GAME_ID + "INTEGER PRIMARY KEY," 
            + COL_GAME_NAME + "TEXT,"
            + COL_KILLS + "TEXT," 
            + COL_DEATHS + "TEXT," 
            + COL_DATE + "TEXT,"
            + COL_MATCH + "TEXT" + ")";

And

String selectWhere = "SELECT * FROM" + TABLE_DEATHTRACK;

Won't work.
Instead, you need to write

    String CREATE_DEATHTRACK_TABLE = 
            "CREATE TABLE " + TABLE_DEATHTRACK + " ("
            + COL_GAME_ID + " INTEGER PRIMARY KEY, " 
            + COL_GAME_NAME + " TEXT, "
            + COL_KILLS + " TEXT, " 
            + COL_DEATHS + " TEXT, " 
            + COL_DATE + " TEXT, "
            + COL_MATCH + " TEXT)";

And

String selectWhere = "SELECT * FROM " + TABLE_DEATHTRACK;

[EDIT]

You are using the reserved KeyWord MATCH.
You better change it to something like Round or Challenge.
For your reference: https://www.sqlite.org/lang_keywords.html

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, Thanks for that but for some reason if you see in my SQLTester.java it still wont show me the data being inputted into the db using the logcat...Any ideas
I didn't notice you are using the reserved KeyWord MATCH. You better change it to something like Round or Challenge
Do you means COL_MATCH or "= ?", because COL_MATCH is just a column name
private static final String COL_MATCH = "Match"; Match is a reserved KeyWord. You can't have a column with that name.
Cheers! Wouldnt have noticed that. Is there anyway to check if values are being added to the sql database ? @Der Golem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.