My problem is that when i press save or load button ,the app crash.
log cat
E/ AndroidRuntime(4101): FATAL EXCEPTION: main
E /AndroidRuntime(4101):android.database.sqlite.SQLiteException: no such table:
mytable:while compiling: INSERT INTO mytable(email, name) VALUES(?, ?);
E/AndroidRuntime(4101):at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
E/AndroidRuntime(4101):atandroid.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
E/AndroidRuntime(4101):at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
E/AndroidRuntime(4101):at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
E/AndroidRuntime(4101):at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
E/AndroidRuntime(4101):android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1150)
E/AndroidRuntime(4101):atandroid.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1576)
E/AndroidRuntime(4101):atandroid.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1456)
E/AndroidRuntime(4101): at com.databasedemo.DataHandler.insertData(DataHandler.java:65)
E/AndroidRuntime(4101):at com.databasedemo.MainActivity$1.onClick(MainActivity.java:37)
E/AndroidRuntime(4101):at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(4101):at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(4101):at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(4101):at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(4101):at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(4101):at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(4101):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4101):at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(4101):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
E/AndroidRuntime(4101):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E/AndroidRuntime(4101): at dalvik.system.NativeStart.main(Native Method)
Below is MainActivity.java
package com.databasedemo;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button save,load;
EditText name,email;
DataHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save =(Button)findViewById(R.id.save);
load=(Button) findViewById(R.id.load);
email=(EditText) findViewById(R.id.email);
name=(EditText) findViewById(R.id.name);
save.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getName=name.getText().toString();
String getEmail=email.getText().toString();
handler=new DataHandler(getBaseContext());
handler.open();
long id=handler.insertData(getName, getEmail);
Toast.makeText(getBaseContext(), "DATA INSERTED", Toast.LENGTH_LONG).show();
handler.close();
}
});
load.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String getName,getEmail;
getName="";
getEmail="";
handler=new DataHandler(getBaseContext());
handler.open();
Cursor c=handler.returnData();
if(c.moveToFirst())
{
do{
getName=c.getString(0);
getEmail=c.getString(1);
}while(c.moveToNext());
}
handler.close();
Toast.makeText(getBaseContext(), "Name:"+getName+" Email:"+getEmail, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
DatabaseHandler .Java
package com.databasedemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.*;
public class DataHandler {
public static final String NAME="name";
public static final String EMAIL="email";
public static final String TABLE_NAME="mytable";
public static final String DATA_BASE_NAME="mydatabase";
public static final int DATABSE_VERSION=1;
public static final String TABLE_CREATE="create table mytable(name text not null,email text not null;)";
DataBaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
this.ctx=ctx;
dbhelper=new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper{
public DataBaseHelper(Context ctx)
{
super(ctx,DATA_BASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion , int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
public DataHandler open()
{
db=dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long insertData(String name,String email){
ContentValues content=new ContentValues();
content.put(NAME,name);
content.put(EMAIL, email);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData()
{
return db.query(TABLE_NAME,new String[]{NAME,EMAIL},null,null,null,null,null);
}
}
Below is activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Enter Email Here"/>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/load"
android:layout_below="@+id/email"
android:layout_marginTop="14dp"
android:text="Save"
android:minWidth="120dp"
android:gravity="center"/>
<Button
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/save"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Load"
android:minWidth="120dp"
android:gravity="center" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:ems="10"
android:hint="Enter Name Here" />
</RelativeLayout>