i m newbie to android following newboston on youtube i stucked here. i checked on SO a few similer questions like this .but could not found any solution to my problem.
need help . while checking on device..when i click on update button it gives an : error is : no such table peopleTable exist. on clicking view button it shows no data.but the SQLView UI that i have set.
HotOrNot class
package com.ss;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class HotOrNot {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "person_name";
public static final String KEY_HOTNESS = "person_hotness";
private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 2;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " + KEY_ROWID
+ " INTEGER PRIMARY KEY, AUTO_INCREMENT ," + KEY_NAME
+ " TEXT NOT NULL , " + KEY_HOTNESS + " TEXT NOT NULL");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot(Context context) {
ourContext = context;
}
public HotOrNot open() throws Exception {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
// called after create method
public long createEntry(String name, String hotness) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Cursor c = ourDatabase.query(DATABASE_TABLE, coloumns, null, null,
null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);
// getting data from the database
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " " + c.getString(iName)
+ " " + c.getString(iHotness) + " \n";
}
return result;
}
public String getName(long l) {
try {
String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Cursor c = ourDatabase.query(DATABASE_TABLE, coloumns, KEY_ROWID
+ " = " + l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
public String getHotness(long l) {
try {
String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Cursor c = ourDatabase.query(DATABASE_TABLE, coloumns, KEY_ROWID
+ " = " + l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String hotness = c.getString(2);
return hotness;
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
public void updateEntry(long lRow, String mName, String mHotness) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME,mName);
cvUpdate.put(KEY_HOTNESS,mHotness);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID +"=" +lRow, null);
}
public void deleteEntry(long lRow1) {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID +"=" +lRow1, null);
}
}
SQLlite example class
package com.ss;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.ss.HotOrNot;
public class SqLiteExample extends Activity implements OnClickListener {
EditText sqlName, sqlHotness;
Button sqlUpdate, sqlView;
Button sqlModify, sqlGetInfo, sqlDelete;
EditText sqlRow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlName = (EditText) findViewById(R.id.etName);
sqlHotness = (EditText) findViewById(R.id.etHotness);
sqlUpdate = (Button) findViewById(R.id.bSqlUpdate);
sqlView = (Button) findViewById(R.id.bSqlOpenView);
sqlUpdate.setOnClickListener(this);
sqlView.setOnClickListener(this);
sqlModify = (Button) findViewById(R.id.bModify);
sqlGetInfo = (Button) findViewById(R.id.bGetInfo);
sqlDelete = (Button) findViewById(R.id.bDeleteEntry);
sqlRow = (EditText) findViewById(R.id.etRow);
sqlModify.setOnClickListener(this);
sqlGetInfo.setOnClickListener(this);
sqlDelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSqlOpenView:
Intent i = new Intent("com.ss.SQLVIEW");
startActivity(i);
break;
case R.id.bSqlUpdate:
boolean didItWork = true;
try {
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
HotOrNot entry = new HotOrNot(SqLiteExample.this);
entry.open();
entry.createEntry(name, hotness);
entry.close();
} catch (Exception e) {
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("ERROR");
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("FINALY");
d.setContentView(tv);
d.show();
}
break;
case R.id.bGetInfo:
didItWork = true;
try {
String s = sqlRow.getText().toString();
long l = Long.parseLong(s); // convert edit text to long
HotOrNot hon = new HotOrNot(this);
try {
hon.open();
} catch (Exception e) {
e.printStackTrace();
}
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();
sqlName.setText(returnedName);
sqlHotness.setText(returnedHotness);
} catch (Exception e) {
// TODO: handle exception
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("catch");
tv.setText(e.toString());
d.setContentView(tv);
d.show();
} finally {
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("FINLAY 2");
d.setContentView(tv);
d.show();
}
break;
case R.id.bModify:
didItWork = true;
try {
String sRow = sqlRow.getText().toString();
long lRow = Long.parseLong(sRow);
String mName = sqlName.getText().toString();
String mHotness = sqlHotness.getText().toString();
HotOrNot ex = new HotOrNot(this);
ex.open();
ex.updateEntry(lRow, mName, mHotness);
ex.close();
} catch (Exception e) {
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("DONE");
tv.setText(error);
d.setContentView(tv);
d.show();
didItWork = true;
} finally {
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("DONE");
d.setContentView(tv);
d.show();
}
break;
case R.id.bDeleteEntry:
didItWork = true;
try {
String sRow1 = sqlRow.getText().toString();
long lRow1 = Long.parseLong(sRow1);
HotOrNot ex1 = new HotOrNot(this);
ex1.open();
ex1.deleteEntry(lRow1);
ex1.close();
} catch (Exception e) {
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("DONE");
tv.setText(error);
d.setContentView(tv);
d.show();
didItWork = true;
} finally {
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
d.setTitle("DONE");
d.setContentView(tv);
d.show();
}
break;
default:
break;
}
}
}
SQLView class
package com.ss;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.TextView;
public class SQLView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.tvSQLInfo);
HotOrNot info = new HotOrNot(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
}
catch (Exception e) {
// TODO Auto-generated catch block
String error = e.toString();
Dialog d = new Dialog(this);
TextView t = new TextView(this);
d.setTitle("ERROR");
t.setText(error);
d.setContentView(t);
d.show();
}
}
}
SQLLite example xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvHotness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hotness Scale 1 t0 10" />
<EditText
android:id="@+id/etHotness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<Button
android:id="@+id/bSqlUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Sql Lite Database" />
<Button
android:id="@+id/bSqlOpenView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View" />
<TextView
android:id="@+id/tvRowId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Row Id"
android:textColor="@android:color/background_dark"
android:textSize="20dp"/>
<EditText
android:id="@+id/etRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/bGetInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Information" />
<Button
android:id="@+id/bModify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Entry" />
<Button
android:id="@+id/bDeleteEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Entry" />
</LinearLayout>
Aucun commentaire:
Enregistrer un commentaire