Reputation: 85
The project consists of two classes one is for the main activity and the another one is Database Helper class that extends Sqliteopenhelper class for executing query on database. The problem occurs when add data button doesn't inserts the data into the database. Probably, there is something wrong with the 'insert data' method, that I am unable to figure out. Or is it something else?
MainActivity.java
public class MainActivity extends Activity {
DatabaseHelper myDb;
EditText editName,editF1,editF2,editF3,editF4,editF5;
Button btnAddData;
Button btnViewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editName = (EditText)findViewById(R.id.editText_name);
editF1 = (EditText)findViewById(R.id.editText_F1);
editF2 = (EditText)findViewById(R.id.editText_F2);
editF3= (EditText)findViewById(R.id.editText_F3);
editF4= (EditText)findViewById(R.id.editText_F4);
editF5= (EditText)findViewById(R.id.editText_F5);
btnAddData = (Button)findViewById(R.id.button_add);
btnViewAll = (Button)findViewById(R.id.button_viewAll);
AddData();
viewAll();
}
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted =
myDb.insertData(editName.getText().toString(), editF1.getText().toString(),
editF2.getText().toString(), editF3.getText().toString(),
editF4.getText().toString(), editF5.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data
Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Name :"+ res.getString(0)+"\n");
buffer.append("Teaching Skills :"+
res.getString(1)+"\n");
buffer.append("Competency :"+
res.getString(2)+"\n");
buffer.append("Approachability :"+
res.getString(3)+"\n");
buffer.append("Class Control :"+
res.getString(4)+"\n");
buffer.append("Punctuality :"+
res.getString(5)+"\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
DatabaseHelper.java
package com.dot.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_2 = "NAME";
public static final String COL_3 = "F1";
public static final String COL_4 = "F2";
public static final String COL_5 = "F3";
public static final String COL_6 = "F4";
public static final String COL_7 = "F5";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (NAME TEXT,F1 TEXT,F2 TEXT,F3
TEXT,F4 TEXT,F5 TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String F1s,String F2s,String
F3s,String F4s,String F5s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,F1s);
contentValues.put(COL_4,F2s);
contentValues.put(COL_5,F3s);
contentValues.put(COL_6,F4s);
contentValues.put(COL_7,F5s);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
Whenever the add data button is clicked data not inserted toast gets invoked. Being a beginner I am unable to figure out where are the things going wrong. Is it something with the insertData method? Please suggest the changes.
Upvotes: 1
Views: 2456
Reputation: 13617
You insertion is correct. You have to query data like this.
if(res.moveToFirst()){
do{
buffer.append("Name :"+ res.getString(0)+"\n");
buffer.append("Teaching Skills :"+ res.getString(1)+"\n");
buffer.append("Competency :"+ res.getString(2)+"\n");
buffer.append("Approachability :"+ res.getString(3)+"\n");
buffer.append("Class Control :"+ res.getString(4)+"\n");
buffer.append("Punctuality :"+ res.getString(5)+"\n\n");
}while (res.moveToNext())
}
In your Database helper class, you forget to close the database after Querying it.
Here is a perfect tutorial to learn SQLite database implementation. http://www.techotopia.com/index.php/An_Android_SQLite_Database_Tutorial
Upvotes: 1