lundi 2 février 2015

Why does my getWritableDataBase return null values?

I am having an issue with my sqlLite database. Here is my code: DatabaseAdapter.java


package autonote.six.padc.autonote;



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

import java.sql.SQLException;

/**
* Created by Ethan on 1/25/2015.
*/
public class DatabaseAdapter {
DatabaseHelper helper;

public DatabaseAdapter(Context context){
helper = new DatabaseHelper(context);
}
public long insertData(String cName, String cTEXT){
SQLiteDatabase db= helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.NAME, cName);
contentValues.put(DatabaseHelper.TEXT,cTEXT );
long id=db.insert(DatabaseHelper.TABLE_NAME,null,contentValues);
return id;

}
public String getAllData(){
SQLiteDatabase db= helper.getWritableDatabase();
String[] columns = {DatabaseHelper.UID, DatabaseHelper.NAME, DatabaseHelper.TEXT};
Cursor cursor=db.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext()){
int cid=cursor.getInt(0);
String name = cursor.getString(1);
String text = cursor.getString(2);
buffer.append(cid + " " + name + " " + text + "<br>");


}
return buffer.toString();
}

class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "DataBase";
public static final String TABLE_NAME = "DATABASE_TABLE";
public static final String UID = "_id";
public static final String NAME = "Name";
public static final int DATABASE_VERSION = 5;
public static final String TEXT = "TEXT";

public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(30),"+TEXT+" VARCHAR(999999));";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS " +TABLE_NAME;

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

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Log.i("Table Created", "Table Created");


}

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

db.execSQL(DROP_TABLE);
onCreate(db);
Log.i("Table Upgraded", "Table Upgraded");

}

}

}


And here is my Load_Text.java



package autonote.six.padc.autotext
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Arrays;


/**
* Created by Ethan on 1/26/2015.
*/
public class Load_Text extends ActionBarActivity {
TextView dataText;
String data;
DatabaseAdapter databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_text);
databaseHelper = new DatabaseAdapter(this);
dataText=(TextView)findViewById(R.id.textView3);
data = databaseHelper.getAllData();

if(databaseHelper.getAllData() == null){
Toast.makeText(this, "No data found", Toast.LENGTH_LONG).show();
}else{
dataText.setText(Html.fromHtml((data)));
}

}


}


The issue I am having here is when onCreate is called the values returned for .getAllData do not return correctly. They return like this:



1 null null
2 null null


This obviously means that name and text are returning null, but, being the newbie to SQLite that I am, I am not quite sure why. Any ideas?


Thanks!


Aucun commentaire:

Enregistrer un commentaire