Storing string value in data base

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Storing string value in data base

Postby Hidayath.Khan » Fri Oct 16, 2009 8:28 am

Hi

I m not able to store string values "ABC" in data base.

i m pasting my code here , pls rectify me.

i have attached the files as well.

package com.khan;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;

public class SQLiteNewDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Step","Step 1");
try {
DBAdapter db = new DBAdapter(this);
Log.d("Step","Step 2");
db.open();
Log.d("Step","Step 1");
System.out.println("INSERTING TWO NEW RECORDS !!");
long id;
id = db.insertTitle(
"0470285818",
"ABC",
"Wrox");
id = db.insertTitle(
"047017661X",
"XYZ",
"Wrox");
db.close();


// Display all titles
System.out.println("DISPLAYING ALL TITLE !!");
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();

// Displaying single title.
System.out.println("DISPLAYIG SINGLE TITLE !!");
db.open();
Cursor c1 = db.getTitle(2);
if (c.moveToFirst())
DisplayTitle(c1);
else
System.out.println("NO TITLE FOUND !!");
db.close();


// Updating titles
System.out.println("UPDATING TITLES !!");
db.open();
if (db.updateTitle(2,
"0470285818",
"J2ME",
"Wrox Press"))
System.out.println("Update successful !!");
else
System.out.println("Update failed !!");
//-------------------
System.out.println("DISPLAYING UPDATED TITLE ");
//---retrieve the same title to verify---
Cursor c2 = db.getTitle(2);
if (c.moveToFirst())
DisplayTitle(c2);
else
System.out.println("NO FILE FOUND !!");
//-------------------
db.close();

// deleting
System.out.println("DELETING TITLE ");
db.open();
if (db.deleteTitle(2))
System.out.println("Deleting successful !!");
else
System.out.println("Deleting failed !!");

db.close();


// Displaying all afterdeleting one record.

System.out.println("Displaying after deleting one record.");
db.open();
Cursor c3 = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c3);
} while (c.moveToNext());
}
db.close();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

public void DisplayTitle(Cursor c)
{
/* Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),
Toast.LENGTH_LONG).show(); */

System.out.println("id:: "+c.getString(0)+" ISBN:: "+c.getString(1)+" TITLE:: " +c.getShort(2)
+" PUBLISHER:: "+c.getShort(3));

}


}






// ONE MORE CLASS






/**
*
*/
package com.khan;

/**
* @author User
*
*/
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.util.Log;

public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_ISBN = "isbn";
public static final String KEY_TITLE = "title";
public static final String KEY_PUBLISHER = "publisher";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "books";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "isbn text not null, title text not null, "
+ "publisher text not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}

//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
DBHelper.close();
}

//---insert a title into the database---
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId)
{

return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;

}

//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

//---updates a title---
public boolean updateTitle(long rowId, String isbn,
String title, String publisher)
{
ContentValues args = new ContentValues();
args.put(KEY_ISBN, isbn);
args.put(KEY_TITLE, title);
args.put(KEY_PUBLISHER, publisher);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}


In place of TITLE & PUBLISHER it displays 0 & more over i m getting some exception don't know the cause.

// THIS IS THE OUTPUT ON CONSOLE.


10-16 12:33:51.871: INFO/System.out(913): INSERTING TWO NEW RECORDS !!
10-16 12:33:51.920: INFO/System.out(913): DISPLAYING ALL TITLE !!
10-16 12:33:51.971: INFO/System.out(913): id:: 2 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.971: INFO/System.out(913): id:: 3 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.981: INFO/System.out(913): id:: 4 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.981: INFO/System.out(913): id:: 5 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.991: INFO/System.out(913): id:: 6 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.991: INFO/System.out(913): id:: 7 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.991: INFO/System.out(913): id:: 8 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:51.991: INFO/System.out(913): id:: 9 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.011: INFO/System.out(913): id:: 10 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.011: INFO/System.out(913): id:: 11 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.021: INFO/System.out(913): id:: 12 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.036: INFO/System.out(913): id:: 13 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.040: INFO/System.out(913): id:: 14 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.040: INFO/System.out(913): DISPLAYIG SINGLE TITLE !!
10-16 12:33:52.081: INFO/System.out(913): id:: 2 ISBN:: 047017661X TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.090: INFO/System.out(913): UPDATING TITLES !!
10-16 12:33:52.130: INFO/System.out(913): Update successful !!
10-16 12:33:52.130: INFO/System.out(913): DISPLAYING UPDATED TITLE
10-16 12:33:52.140: INFO/System.out(913): id:: 2 ISBN:: 0470285818 TITLE:: 0 PUBLISHER:: 0
10-16 12:33:52.151: INFO/System.out(913): DELETING TITLE
10-16 12:33:52.190: INFO/System.out(913): Deleting successful !!
10-16 12:33:52.200: INFO/System.out(913): Displaying after deleting one record.
10-16 12:33:52.240: WARN/System.err(913): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 12
10-16 12:33:52.249: WARN/System.err(913): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
10-16 12:33:52.249: WARN/System.err(913): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
10-16 12:33:52.249: WARN/System.err(913): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
10-16 12:33:52.265: WARN/System.err(913): at com.khan.SQLiteNewDemo.DisplayTitle(SQLiteNewDemo.java:120)
10-16 12:33:52.271: WARN/System.err(913): at com.khan.SQLiteNewDemo.onCreate(SQLiteNewDemo.java:96)
10-16 12:33:52.271: WARN/System.err(913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-16 12:33:52.271: WARN/System.err(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
10-16 12:33:52.271: WARN/System.err(913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
10-16 12:33:52.281: WARN/System.err(913): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-16 12:33:52.294: WARN/System.err(913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
10-16 12:33:52.294: WARN/System.err(913): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 12:33:52.294: WARN/System.err(913): at android.os.Looper.loop(Looper.java:123)
10-16 12:33:52.294: WARN/System.err(913): at android.app.ActivityThread.main(ActivityThread.java:3948)
10-16 12:33:52.294: WARN/System.err(913): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 12:33:52.294: WARN/System.err(913): at java.lang.reflect.Method.invoke(Method.java:521)
10-16 12:33:52.294: WARN/System.err(913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
10-16 12:33:52.294: WARN/System.err(913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
10-16 12:33:52.294: WARN/System.err(913): at dalvik.system.NativeStart.main(Native Method)



:cry:
Attachments
SQLiteNewDemo.java
(3 KiB) Downloaded 51 times
DBAdapter.java
(4.26 KiB) Downloaded 53 times
Hidayath.Khan
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 14, 2009 12:03 pm

Top

Re: Storing string value in data base

Postby Hidayath.Khan » Fri Oct 16, 2009 10:14 am

Hi

i m able to solve the problem by myself, in hurry i did some coding problem, variables usage problem.

I m attaching the code with this reply.

Anyhow thanks for ur views.

Thanks
Hidayath
Attachments
SQLiteNewDemo.java
(3.01 KiB) Downloaded 51 times
DBAdapter.java
(4.26 KiB) Downloaded 53 times
Hidayath.Khan
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 14, 2009 12:03 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google Feedfetcher and 7 guests