Tuesday, November 3, 2009

[android-developers] Database Errors

I been working on this code dealing with my database for days i can
figure out whats wrong.

Heres my code:
package com.helpihelpyou;


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.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.provider.Contacts.People;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;


public class HelpiDB {

private static class HelpiDBHelper extends SQLiteOpenHelper {
Context ctx;

public HelpiDBHelper(Context context, String name, CursorFactory
factory, int version) {
super(context, name, factory, version);
this.ctx = context;
}

/*
* Called when no database exists in disk and the helper class needs
to
* create one.
*/
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
try {
// Form an array specifying which columns to return.
String[] projection = new String[] { People._ID, People.NAME,
People.NUMBER,

};

// Get the base URI for the People table in the Contacts content
// provider.
Uri contacts = People.CONTENT_URI;

// Make the query. To get the data

Cursor c = ctx.getContentResolver().query(contacts, projection,
null, null,
null);

int numRows = c.getCount();
c.isFirst();
c.moveToFirst();

for (int i = 0; i < numRows; ++i) {
Row row = new Row();
row._id = c.getLong(0);
row.name = c.getString(1);
row.number = c.getString(2);
ContentValues initialValues = new ContentValues();
initialValues.put("name", row.name);
initialValues.put("number", row.number);
Boolean check = false;
initialValues.put("check", check.toString());

db.insertOrThrow(DATABASE_TABLE, null, initialValues);
c.moveToNext();
}
} catch (SQLiteException e) {
e.printStackTrace();
}
}

/*
* Upgrade the existing database to conform to the new version
Multiple
* previous versions can be handled by comparing _oldVersion and
* _newVersion values.
*
* The simplest case is to drop the old table and create a new one
*/

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}

}

public static final int CHECK_COLUMN = 3;

public static final String KEY_CHECK = "check";
// The index (key) column name for use in where clauses.
public static final String KEY_ID = "_id";

// The name and column index of each column in your database.
public static final String KEY_NAME = "name";

public static final String KEY_NUMBER = "number";

public static final int NAME_COLUMN = 1;
public static final int NUMBER_COLUMN = 2;

private static final String DATABASE_CREATE = "create table "
+ "HELPITABLE"
+ " (_id integer primary key autoincrement, "
+ "name text not null, "
+ "number text not null, "
+ "check text not null" + ");";

private static final String DATABASE_NAME = "HELPIDB";
private static String DATABASE_PATH = "/data/data/com.helpihelpyou/
databases/";

private static final String DATABASE_TABLE = "HELPITABLE";

private static final int DATABASE_VERSION = 1;

private final Context ctx;

private SQLiteDatabase db;

private final HelpiDBHelper dbHelper;

public HelpiDB(Context context) {
this.ctx = context;
ctx.deleteDatabase(DATABASE_NAME);
dbHelper = new HelpiDBHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);

}

public boolean checkDataBase() {

SQLiteDatabase checkDB = null;

try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

} catch (SQLiteException e) {

// database does't exist yet.

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;
}

public void close() {
if (db != null) {
db.close();
}

}

public void create() throws SQLiteException {
boolean dbExist = checkDataBase();
if (dbExist) {
// If db exist do nothing
} else {
db = ctx.openOrCreateDatabase(DATABASE_NAME, 0, null);
db.execSQL(DATABASE_CREATE);
db = ctx.openOrCreateDatabase(DATABASE_NAME, 0, null);

// Form an array specifying which columns to return.
String[] projection = new String[] { People._ID, People.NAME,
People.NUMBER,

};

// Get the base URI for the People table in the Contacts content
// provider.
Uri contacts = People.CONTENT_URI;

// Make the query. To get the data

Cursor c = ctx.getContentResolver().query(contacts, projection,
null, null, null);

int numRows = c.getCount();
c.isFirst();
c.moveToFirst();

for (int i = 0; i < numRows; ++i) {
Row row = new Row();
row._id = c.getLong(0);
row.name = c.getString(1);
row.number = c.getString(2);
createRow(row.name, row.number, false);
c.moveToNext();
}
}
}

public void createRow(String name, String number, Boolean check) {
ContentValues initialValues = new ContentValues();
initialValues.put("name", name);
initialValues.put("number", number);
initialValues.put("check", check.toString());

db.insertOrThrow(DATABASE_TABLE, null, initialValues);

}

public void deleteRow(long rowId) {
db.delete(DATABASE_TABLE, "_id=" + rowId, null);
}

public List<Row> fetchAllRows() {
ArrayList<Row> ret = new ArrayList<Row>();
try {
Cursor c = db.query(DATABASE_TABLE, new String[] { "_id", "name",
"number",
"email", "owner" }, null, null,
null, null, null);
int numRows = c.getCount();
c.isFirst();
c.moveToFirst();
for (int i = 0; i < numRows; ++i) {
Row row = new Row();
row._id = c.getLong(0);
row.name = c.getString(1);
row.number = c.getString(2);
ret.add(row);
c.moveToNext();
}
} catch (SQLException e) {
Log.e("Exception on query", e.toString());
}
return ret;
}

public Cursor GetAllRows() {
try {
return db.query(DATABASE_TABLE, new String[] { "_id", "name",
"number", "check" },
null, null, null, null, null);
} catch (SQLException e) {
Log.i("Error on query", e.toString());
return null;
}

}

public HelpiDB open() throws SQLiteException {
db = dbHelper.getWritableDatabase();
return this;

}

public void updateRow(long rowId, String name, String number,
Boolean check) {
ContentValues args = new ContentValues();
args.put("name", name);
args.put("number", number);
args.put("check", check.toString());
db.update(DATABASE_TABLE, args, "_id=" + rowId, null);
}

}

I keep getting this error
11-03 17:54:37.302: WARN/System.err(1027):
android.database.sqlite.SQLiteException: near "check": syntax error: ,
while compiling: INSERT INTO HELPITABLE(check, number, name) VALUES
(?, ?, ?);

11-03 17:54:37.452: ERROR/AndroidRuntime(1027): Caused by:
java.lang.NullPointerException
11-03 17:54:37.452: ERROR/AndroidRuntime(1027): at
com.helpihelpyou.InsertActivity.onCreate(InsertActivity.java:60)
the error is on this line:

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment