SQLite database

0 nazia ch · May 19, 2015
i am facing problems with sql database.following exactly buckys tuts for android development but my app is not able to view stored records when i click on view app crashed.can u plz help me in this matter?
if there is anyone i can send him project
thnkx

Post a Reply

Replies

Oldest  Newest  Rating
+1 Ian Arbuckle · May 22, 2015
Have you got your source code?
0 nazia ch · May 24, 2015
yes Ian i have source code 
0 Kushagra Jaiswal · May 25, 2015
Same problem.. did exactly the same but app not running in emulator.. in the log screen however there were many processes going on.. but the batabase did not display. Also some SQL code which i ran by learning from MySQL databases did not run (AUTO_INCREMENT and the use of '  ' single quotes to give product name while writing query)... so could anyone help me with this? I have not been able to proceed further and the good part is coming..
0 Ian Arbuckle · May 25, 2015
If I can have a look at it and find the source of the problem.
0 nazia ch · May 26, 2015
i can send u whole project but how?
0 Ian Arbuckle · May 26, 2015
You can just copy/paste the code here, I don't need the project file. Use the code tag to help me read it :P

It should look like this.
0 Mazhar Ahmad · May 26, 2015
Nazia please post your code so we can have a look.

Thanks
0 nazia ch · May 29, 2015
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {

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);
d.setTitle("dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

} finally {
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Heck Yea!");
TextView tv = new TextView(this);
tv.setText("success");
d.setContentView(tv);
d.show();
}

}
break;

case R.id.bSQLView:
Intent i = new Intent("com.example.thenewbosten.VIEW");
startActivity(i);

 
break;

case R.id.bgetInfo:
try {
String s = sqlRow.getText().toString();
long l = Long.parseLong(s);
HotOrNot hon = new HotOrNot(this);
hon.open();
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();

sqlName.setText(returnedName);
sqlHotness.setText(returnedHotness);

} catch (Exception e) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

}
break;

case R.id.bSQLmodify:
try {
String mName = sqlName.getText().toString();
String mHotness = sqlHotness.getText().toString();
String sRow = sqlRow.getText().toString();
long lRow = Long.parseLong(sRow);

HotOrNot ex = new HotOrNot(this);
ex.open();
ex.updateEntry(lRow, mName, mHotness);
ex.close();

} catch (Exception e) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
break;

case R.id.bSQLdelete:
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) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
break;

}

}
public class HotOrNot {

// rows
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";

// database
private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

// instance of bellow class
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);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// SETTING UP DATABASE
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + "TEXT NOT NULL );"

);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
onCreate(db);

}

}

// Constructor for main class
public HotOrNot(Context c) {
ourContext = c;
}

public HotOrNot open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}

public void close() {
ourHelper.close();
}

public long createEntry(String name, String hotness) {
// TODO Auto-generated method stub
// bundle is like put something in it and insert it into database
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public String getData() {
// TODO Auto-generated method stub
String[] columns= new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, 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);

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) throws SQLException {
// TODO Auto-generated method stub
String[] columns= new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c =ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if( c!= null){
c.moveToFirst();
String name= c.getString(1);
return name;
}
return null;
}

public String getHotness(long l) throws SQLException {
// TODO Auto-generated method stub
String[] columns= new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c =ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if( c!= null){
c.moveToFirst();
String hotness= c.getString(2);
return hotness;
}
return null;
}

public void updateEntry(long lRow, String mName, String mHotness) throws SQLException {
// 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) throws SQLException {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);


}

}
0 Utkarsh Pateriya · May 22, 2015
i'm also Facing the same problem do anyone Knows how to fix that App Crash Problem?
0 Abdullah Afzal · May 23, 2015
It's basically the connection problem with your server. Make sure you've authority to store data in authoritative files
  • 1

Java / Android Development

107,272 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator