I'm making a database for storing the information of the contacts. Because I'm new to android, I used a code found from the Internet. However, it has errors and when run it app crashes.
Here is the code for database :
public class FriendStorage extends SQLiteOpenHelper {
public FriendStorage(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
}
public static final String Database_name = "net.Friend.db";
public static final int Database_version = 1;
public static final String _ID = "_id";
public static final String Table_Friend = "Table_friend";
public static final String Username = "username";
public static final String PhoneNumber = "phoneNumber";
private static final String TABLE_FRIEND_CREATE =
"CREATE TABLE " + Table_Friend
+ " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Username + " VARCHAR(25), "
+ PhoneNumber + " VARCHAR(25)";
public static final String Table_Friend_Drop = "DROP TABLE IF EXISTS" + Table_Friend;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_FRIEND_CREATE);
}
public void insert(String username, String phoneNumber){
long rowId;
try {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Username, username);
contentValues.put(PhoneNumber, phoneNumber);
rowId = db.insert(Table_Friend, null, contentValues);
}catch (Exception e){
e.printStackTrace();
}
}
public Cursor get(){
Cursor cursor;
SQLiteDatabase db = getReadableDatabase();
String selectQuery = "SELECT * FROM" + Table_Friend;
cursor = db.rawQuery(selectQuery, null);
return cursor;
}
public Cursor Update(String preUsername, String prePhoneNumber, String newUsername, String newPhoneNumber){
Cursor cursor;
SQLiteDatabase db = getWritableDatabase();
String updateTable = "UPDATE" + Table_Friend + "SET" + Username + " = " + newUsername
+ "AND" + PhoneNumber + " = " + newPhoneNumber + "WHERE" + Username + " = "
+ preUsername + "AND" + PhoneNumber + " = " + prePhoneNumber;
cursor = db.rawQuery(updateTable, null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(Table_Friend_Drop);
}
}
and in the Main Activity i use get function to search in database.
FriendStorage friendInfoStorage = new FriendStorage(this, "net.Friend.db", null, 1);
cursor = friendInfoStorage.get();
int noOfFriends = 0;
if (cursor.getCount() > 0) {
cursor.moveToFirst();
while ((!cursor.isAfterLast()) && (noOfFriends < cursor.getCount())) {
if (noOfFriends == 0) {
Myself.setUsername(cursor.getString(1));
Myself.setPhone_Number(cursor.getString(2));
} else {
User.setUsername(cursor.getString(1));
User.setPhone_Number(cursor.getString(2));
Friends.add(User);
}
noOfFriends++;
cursor.moveToNext();
}
}
friendInfoStorage.close();
Error is this :
07-12 03:11:14.024 14157-14157/net.network.alire.clientchat E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.network.alire.clientchat, PID: 14157
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.network.alire.clientchat/net.network.alire.clientchat.MainActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Table_friend (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(25), phoneNumber VARCHAR(25)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Table_friend (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(25), phoneNumber VARCHAR(25)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at net.network.alire.toolBox.FriendStorage.onCreate(FriendStorage.java:36)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at net.network.alire.toolBox.FriendStorage.get(FriendStorage.java:56)
at net.network.alire.clientchat.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Can anyone tell me what is wrong with the code?
thanks in advance
答案 0 :(得分:1)
There is a ")" missing caracter in your create statement.
You should use TEXT instead of varchar with SQLite.
You should not use AUTOINCREMENT. It is implicitly implemented in the PRIMARY KEY with SQLite.