lundi 13 juillet 2015

Error when trying to put information into SQLite Database for Android

public class DatabaseOperations extends SQLiteOpenHelper {
public static final int DATABASE_VERSION =1;
public String CREATE_QUERY = "CREATE TABLE "+ TableData.TableInfo.TABLE_NAME + " (" + TableData.TableInfo.FACT_+" TEXT"+ TableData.TableInfo.RATING_VALUE+" INTEGER);";

public DatabaseOperations(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
    Log.d("Database operations","Database created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    sdb.execSQL(CREATE_QUERY);
    Log.d("Database operations","Table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void putInformation(DatabaseOperations dop, String fact, int rating){

    SQLiteDatabase SQ = dop.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put(TableData.TableInfo.FACT_,fact);
    cv.put(TableData.TableInfo.RATING_VALUE ,rating);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("Database operations", "One row inserted");


}

public Cursor getInformation(DatabaseOperations dop){
    SQLiteDatabase SQ = dop.getReadableDatabase();
    String[] coloumns ={TableData.TableInfo.FACT_, TableData.TableInfo.RATING_VALUE};
    Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME,coloumns,null,null,null,null,null);
    return CR;
}

}

public class FunFactsActivity extends ActionBarActivity implements View.OnClickListener {

private FactBook mFactBook = new FactBook();
Context ctx = this;
DatabaseOperations DB = new DatabaseOperations(ctx);
Cursor CR = DB.getInformation(DB);


@Override
protected void onCreate(Bundle savedInstanceState) {

   //
   // cr.moveToFirst();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);

    // Declare new variable and assign the views from the layout file
    Button showFactButton = (Button) findViewById(R.id.showfactbutton);
    Button upVoteButton = (Button) findViewById(R.id.upVoteButton);
    Button downVoteButton = (Button) findViewById(R.id.downVoteButton);

    showFactButton.setOnClickListener(this);
    upVoteButton.setOnClickListener(this);
    downVoteButton.setOnClickListener(this);

    storeFactsAndRatings();
}


@Override
public void onClick(View v) {

    switch (v.getId()){
        case R.id.showfactbutton:
            displayFact();
            break;

        case R.id.upVoteButton:
          //  upVote();
            displayFact();
            break;

        case R.id.downVoteButton:
         //   downVote();
            displayFact();
            break;
    }
}

public void displayFact(){
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    final TextView factLabel = (TextView) findViewById(R.id.factextview);
    final TextView ratingLabel = (TextView) findViewById(R.id.rating);
    final String[] color = {"#39add1","#3079ab","#c25975","#e15258","#838cc7","#7d669e","#53bbb4","#51b46d"};
    String fact;
    int rating;
    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(color.length);
    //update the label with our dynamic fact
    CR.moveToPosition(randomNumber);
    fact = CR.getString(0);
    rating = CR.getInt(1);
    factLabel.setText(fact);
    ratingLabel.setText(rating + "");
    relativeLayout.setBackgroundColor(Color.parseColor(color[randomNumber]));
}
public void upVote(){
    //DB.putInformation(DB,null, cr.getInt(1)+1);
}

public void downVote(){
  //  DB.putInformation(DB,null, cr.getInt(1)-1);
}

public void storeFactsAndRatings(){
    for(int i=0;i<11;i++) {
        DB.putInformation(DB, mFactBook.storeFacts(), mFactBook.storeRatings());
    }
    Toast.makeText(getBaseContext(),"Database Complete", Toast.LENGTH_LONG).show();
}

}

I commented out everything to debug to figure out that its when im trying to get information that it crashes, this is the first app that im building ever, and cant seem to get past this issue. My laptop doesn't even seem to be able to run an arm emulator so using log messages to try to debug cant be done. Any help would be great! Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire