从表中记录没有被删除sqlite android

时间:2014-08-04 13:19:09

标签: android android-sqlite

我已经使用数据库来存储用户信息。但是我想从联系人中删除特定信息。我已经为它创建了一个方法,但每当我调用该方法时,我都会收到Sql错误。

代码

public class GroupDataBase extends SQLiteOpenHelper {

        private static final int dbVersion = 1;
        private static final String dbName = "HSsuraksha";
        private static final String grouptableName = "groupDetails";
        private static final String contacttableName = "contactDetails";
        private static final String groupId = "groupId";
        private static final String groupName = "groupName";
        private static final String createdOn = "createdOn";
        private static final String contactId = "contactId";
        private static final String contactGroupId = "groupId";
        private static final String contactName = "contactName";
        private static final String contactNumber = "contactNumber";
        private String groupIdValue;
        private SQLiteDatabase database;
        private ArrayList<String> groupIdList;
        private ArrayList<GroupModel> getGroupInfo;
        private ArrayList<String> alContactMembers;
        private static final String createContactTable = "Create Table " + contacttableName + "(" + contactId + " Integer Primary Key AutoIncrement," + contactGroupId + " Text," + contactName + " Text," + contactNumber + " Text" + ");";
        private static final String createGroupTable = "CREATE TABLE " + grouptableName + "(" + groupId + " Integer Primary Key AutoIncrement," + groupName + " Text," + createdOn + " Text" + ");";


        public GroupDataBase(Context context) {
            super(context, dbName, null, dbVersion);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {

            sqLiteDatabase.execSQL(createGroupTable);
            sqLiteDatabase.execSQL(createContactTable);

        }

public void deleteContact(String id, String strcontactName) {
        String query = "Delete " + contactName + "," + contactNumber + " FROM " + contacttableName + " where" + groupId + "=" + id + " AND " + contactName + "=" + strcontactName;
        database = getWritableDatabase();
        database.rawQuery(query, null);
        database.close();
    }

logcat的

08-04 18:43:03.758  32336-32336/example.com.pocketdocs E/SQLiteLog﹕ (1) near "contactName": syntax error
08-04 18:43:03.788  32336-32336/example.com.pocketdocs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: near "contactName": syntax error (code 1): , while compiling: Delete contactName,contactNumber FROM contactDetails wheregroupId=2 AND contactName=Ganesh ABC
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1322)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1261)
            at example.com.pocketdocs.DataBase.GroupDataBase.deleteContact(GroupDataBase.java:159)
            at example.com.pocketdocs.Group.GroupDetailsFragments.onItemLongClick(GroupDetailsFragments.java:58)
            at android.widget.AbsListView.performLongPress(AbsListView.java:2925)
            at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2875)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

不在删除脚本中插入列名。改变你的脚本:

String query = "Delete FROM " + contacttableName + " where" + groupId + "=" + id + " AND " + contactName + "=" + strcontactName;

谢谢!

答案 1 :(得分:0)

问题是您的删除查询错误,您无法删除列。 示例删除查询: DELETE FROM contactDetails WHERE some_contactId = 1;

如果只想更改一列,则必须使用更新查询。