mardi 3 mars 2015

Android SQLIte No Such Table Error - On Click

Good afternoon,


I am trying to create a database for a monthly assessment form as part of my, it will be the second table of my application first being login details.


I followed the same format but am getting "NO SUCH TABLE" followed by my variable name.


I was hoping this issue had been seen before and how it could be eradicated.


Here is my code for the java files:


MonthlyDataBaseAdapter:



package android.one.two;

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

public class MonthlyDataBaseAdapter
{
static final String DATABASE_NAME = "MonthlyAssessment.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"MONTHLYASSESSMENT"+
"( " +"ID"+" integer primary key autoincrement,"+ "STUDENTNUMBER real,CURRENTWEIGHT text,CURRENTBMI real,TARGETWEIGHT real,COMMENTS real); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public MonthlyDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public MonthlyDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String studentNumber,String currentweight, String currentbmi, String targetweight, String comments)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("STUDENTNUMBER",studentNumber);
newValues.put("CURRENTWEIGHT",currentweight);
newValues.put("CURRENTBMI",currentbmi);
newValues.put("TARGETWEIGHT",targetweight);
newValues.put("COMMENTS",comments);


// Insert the row into your table
db.insert("MONTHLYASSESSMENT", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}


public void updateEntry(String studentNumber,String currentweight, String currentbmi, String targetweight, String comments)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("STUDENTNUMBER",studentNumber);
updatedValues.put("CURRENTWEIGHT",currentweight);
updatedValues.put("CURRENTBMI",currentbmi);
updatedValues.put("TARGETWEIGHT",targetweight);
updatedValues.put("COMMENTS",comments);


String where="STUDENTNUMBER = ?";
db.update("MONTHLYASSESSMENT",updatedValues, where, new String[]{studentNumber});
}
}


MonthlyAssessment:



package android.one.two;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidapp.fyp.uccpocketcoach.R;

public class MonthlyAssessment extends Activity
{
EditText editTextConfirmStudentNumber,editTextMonthEndWeight,editTextMonthEndBMI, editTextNextWeight, editTextComment;
Button btnSubmit;

MonthlyDataBaseAdapter monthlyDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monthly_assessment);

// get Instance of Database Adapter
monthlyDataBaseAdapter=new MonthlyDataBaseAdapter(this);
monthlyDataBaseAdapter=monthlyDataBaseAdapter.open();

// Get References of Views
editTextConfirmStudentNumber=(EditText)findViewById(R.id.editTextConfirmStudentNumber);
editTextMonthEndWeight=(EditText)findViewById(R.id.editTextMonthEndWeight);
editTextMonthEndBMI=(EditText)findViewById(R.id.editTextMonthEndBMI);
editTextNextWeight=(EditText)findViewById(R.id.editTextNextWeight);
editTextComment=(EditText)findViewById(R.id.editTextComment);


btnSubmit=(Button)findViewById(R.id.buttonSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

String studentNumber=editTextConfirmStudentNumber.getText().toString();
String currentweight=editTextMonthEndWeight.getText().toString();
String currentbmi=editTextMonthEndBMI.getText().toString();
String targetweight=editTextNextWeight.getText().toString();
String comments=editTextComment.getText().toString();


// check if any of the fields are vacant
if(studentNumber.equals("")||currentweight.equals("")||currentbmi.equals("")||targetweight.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}

else
{
// Save the Data in Database
monthlyDataBaseAdapter.insertEntry(studentNumber, currentweight, currentbmi, targetweight, comments);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();

Intent ii=new Intent(MonthlyAssessment.this,MainMenu.class);
startActivity(ii);
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

monthlyDataBaseAdapter.close();
}
}


Error Log:



03-03 19:19:13.401: E/SQLiteLog(8755): (1) no such table: MONTHLYASSESSMENT
03-03 19:19:13.402: E/SQLiteDatabase(8755): Error inserting STUDENTNUMBER=123456789 TARGETWEIGHT=79 CURRENTBMI=21.4 CURRENTWEIGHT=77 COMMENTS=Found tricep dips hard
03-03 19:19:13.402: E/SQLiteDatabase(8755): android.database.sqlite.SQLiteException: no such table: MONTHLYASSESSMENT (code 1): , while compiling: INSERT INTO MONTHLYASSESSMENT(STUDENTNUMBER,TARGETWEIGHT,CURRENTBMI,CURRENTWEIGHT,COMMENTS) VALUES (?,?,?,?,?)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.fyp.uccpocketcoach.MonthlyDataBaseAdapter.insertEntry(MonthlyDataBaseAdapter.java:56)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.fyp.uccpocketcoach.MonthlyAssessment$1.onClick(MonthlyAssessment.java:59)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.view.View.performClick(View.java:4756)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.view.View$PerformClick.run(View.java:19749)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.os.Handler.handleCallback(Handler.java:739)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.os.Handler.dispatchMessage(Handler.java:95)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.os.Looper.loop(Looper.java:135)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at java.lang.reflect.Method.invoke(Native Method)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at java.lang.reflect.Method.invoke(Method.java:372)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-03 19:19:13.402: E/SQLiteDatabase(8755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-03 19:19:13.587: D/OpenGLRenderer(8755): endAllStagingAnimators on 0xa177f200 (RippleDrawable) with handle 0xa1ca6e30


Have tried uninstalling and reinstalling apk on device but it has failed also


Any help would be extremely helpful!


Aucun commentaire:

Enregistrer un commentaire