简体   繁体   English

升级后SQLitedatabase崩溃

[英]SQLitedatabase crash after upgrade

I added 3 new columns to my Database from version 1 so I changed it to version 2 but now when I use an activity that uses my database my app crashes and shows this in the logcat. 我从版本1向数据库添加了3个新列,因此将其更改为版本2,但是现在当我使用使用数据库的活动时,我的应用程序崩溃并在logcat中显示。

04-02 18:41:09.013: E/AndroidRuntime(19171): FATAL EXCEPTION: main 04-02 18:41:09.013: E/AndroidRuntime(19171): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fullfrontalgames.numberfighter/com.fullfrontalgames.numberfighter.AccountSettings}: android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: create table NFDB( ID integer primary key autoincrement,USERNAME text CREATE UNIQUE INDEX idx_keytype ON tableName (USERNAME);USERNAME text,PASSWORD text,EMAIL text,NUMBERINPUT text,SCORE text,FRIENDS text); 04-02 18:41:09.013:E / AndroidRuntime(19171):致命异常:主04-02 18:41:09.013:E / AndroidRuntime(19171):java.lang.RuntimeException:无法启动活动ComponentInfo {com。 fullfrontalgames.numberfighter / com.fullfrontalgames.numberfighter.AccountSettings}:android.database.sqlite.SQLiteException:在“ CREATE”附近:语法错误(代码1):,在编译时:创建表NFDB(ID整数主键自动递增,USERNAME文本CREATE唯一索引idx_keytype ON tableName(USERNAME); USERNAME文本,PASSWORD文本,EMAIL文本,NUMBERINPUT文本,SCORE文本,FRIENDS文本);

04-02 18:41:09.013: E/AndroidRuntime(19171): Caused by: android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: create table NFDB( ID integer primary key autoincrement,USERNAME text CREATE UNIQUE INDEX idx_keytype ON tableName (USERNAME);USERNAME text,PASSWORD text,EMAIL text,NUMBERINPUT text,SCORE text,FRIENDS text); 04-02 18:41:09.013:E / AndroidRuntime(19171):原因:android.database.sqlite.SQLiteException:在“ CREATE”附近:语法错误(代码1):,在编译时:创建表NFDB(ID整数主密钥自动递增,USERNAME文本,在tableName(USERNAME)上创建唯一索引idx_keytype; USERNAME文本,PASSWORD文本,EMAIL文本,NUMBERINPUT文本,SCORE文本,FRIENDS文本);

I can't see the syntax error anywhere and I have my DBHelper helperclass to drop my old table and make the new table. 我在任何地方都看不到语法错误,并且使用DBHelper helperclass删除旧表并创建新表。

here is my code of my DBAdapter and DBHelper classes 这是我的DBAdapter和DBHelper类的代码

    package com.fullfrontalgames.numberfighter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DBAdapter
{
        static final String DATABASE_NAME = "NFDB.db";
        static final int DATABASE_VERSION = 2;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        static final String DATABASE_CREATE = "create table "+"NFDB"+
                                     "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text CREATE UNIQUE INDEX idx_keytype ON tableName (USERNAME);" +
                                            "PASSWORD text,EMAIL text,NUMBERINPUT text,SCORE text,FRIENDS text); "; 
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper DBHelper;
        public  DBAdapter(Context _context)
        {
            context = _context;
            DBHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  DBAdapter open() throws SQLException
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }
        public void close()
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String userName,String password,String email)
        {
           ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", userName);
            newValues.put("PASSWORD",password);
            newValues.put("EMAIL", email);
            // Insert the row into your table
            db.insert("NFDB", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String userName)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("NFDB", where, new String[]{userName}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }    
        public String getSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("NFDB", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;

        }
        public void  updateEntry(String userName,String password)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put("USERNAME", userName);
            updatedValues.put("PASSWORD",password);


            String where="USERNAME = ?";
            db.update("NFDB",updatedValues, where, new String[]{userName});              
        }
        public String getData() {
            String[] columns = new String[] { "ID", "USERNAME"};
            Cursor c = db.query("NFDB", columns, null, null, null, null, null, null);
            String result ="";
            int iRow = c.getColumnIndex("ID");
            int iName = c.getColumnIndex("USERNAME");

            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                result = result + c.getString(iRow) + " " + c.getString(iName) + "/n";
            }

            return result;
        }

        public String getUsername(String searchName) {
            Cursor c = db.query("NFDB",
                                new String[] { "USERNAME" },
                                "USERNAME = ?",
                                new String[] { searchName },
                                null, null, null);
            if (c.moveToNext())
                return c.getString(0);
            else
                return "";
        }
        public void InsertScore(String Username,String Score)
        {
            ContentValues ScoreValues = new ContentValues();
            ScoreValues.put("USERNAME", Username);
            ScoreValues.put("SCORE", Score);
            db.insert("NFDB", null, ScoreValues);

        }
        public String GetGameScore(String Username,String Score)
        {
            Cursor cursor = db.query("NFDB", null, "USERNAME",new String[]{Username,Score}, null, null, null);
            cursor.moveToFirst();
            cursor.close();
            return Username;
        }
        public String GetAllScore()
        {
            String[] columns = new String[] {"ID","USERNAME","SCORE"};
            Cursor cursor = db.query("NFDB", columns, null, null, null, null, null);
            String result="";
            int iRow = cursor.getColumnIndex("ID");
            int iUsername = cursor.getColumnIndex("USERNAME");
            int iScore = cursor.getColumnIndex("SCORE");

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                result = result + cursor.getString(iRow) + " " + cursor.getString(iUsername) + " " + cursor.getString(iScore) + "/n";
            }
            return result;
            }

        public void InsertNumber(String Username,String Number)
        {
            ContentValues NumberValues = new ContentValues();
            NumberValues.put("USERNAME", Username);
            NumberValues.put("NUMBERINPUT", Number);
            db.insert("NFDB", null, NumberValues);
            }

        public String GetNumber(String Username,String Number)
        {
            Cursor cursor = db.query("NFDB", null, "USERNAME", new String[]{Username,Number}, null, null, null, null);
            cursor.moveToFirst();
            cursor.close();
            return Username;
        }

        public String GetAllNumbers()
        {
            String[] columns = new String[] {"ID","USERNAME","NUMBERINPUT"};
            Cursor cursor = db.query("NFDB", columns, null, null, null, null, null, null);
            String result="";
            int iRow = cursor.getColumnIndex("ID");
            int iName = cursor.getColumnIndex("USERNAME");
            int iNumber = cursor.getColumnIndex("NUMBERINPUT");

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + " " + cursor.getString(iNumber) + "/n";
            }
            return result;

            }
        public void InsertFriends (String Username,String Friends)
        {
            ContentValues FriendValues = new ContentValues();
            FriendValues.put("USERNAME", Username);
            FriendValues.put("FRIENDS", Friends);
            db.insert("NFDB", null, FriendValues);
        }

        public int DeleteFriends(String Username,String Friends)
        {
            String where = "USERNAME=?,FRIENDS=?";
            int numberOfEntriesDeleted = db.delete("NFDB", where, new String[]{Username,Friends});
            return numberOfEntriesDeleted;
        }

        public String GetFriend(String Username,String Friend)
        {
            Cursor cursor = db.query("NFDB", null, "USERNAME", new String[]{Username,Friend}, null, null, null, null);
            cursor.moveToFirst();
            cursor.close();
            return Username;
        }


        public String GetAllFriends()
        {
            String[] columns = new String[] {"ID","USERNAME","FRIENDS"};
            Cursor cursor = db.query("NFDB", columns, null, null, null, null, null, null);
            String result="";
            int iRow = cursor.getColumnIndex("ID");
            int iName = cursor.getColumnIndex("USERNAME");
            int iFriends = cursor.getColumnIndex("FRIENDS");

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + " " + cursor.getString(iFriends) + "/n";
            }
            return result;

            }


        }

package com.fullfrontalgames.numberfighter; 包com.fullfrontalgames.numberfighter;

 import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DataBaseHelper extends SQLiteOpenHelper { public DataBaseHelper(Context context, String name,CursorFactory factory, int version) { super(context, name, factory, version); } // TODO Auto-generated constructor stub // Called when no database exists in disk and the helper class needs // to create a new one. @Override public void onCreate(SQLiteDatabase _db) { _db.execSQL(DBAdapter.DATABASE_CREATE); } // Called when there is a database version mismatch meaning that the version // of the database on disk needs to be upgraded to the current version. @Override public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) { // Log the version upgrade. Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data"); // 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. _db.execSQL("DROP TABLE IF EXISTS " + "NFDB"); // Create a new one. onCreate(_db); } } 

Your DML statement is incorrect. 您的DML语句不正确。 Index creation cannot be used in CREATE TABLE statement. 索引创建不能在CREATE TABLE语句中使用。 It must have its own statement. 它必须有自己的声明。

You have to correct it like this: 您必须像这样纠正它:

DATABASE_CREATE = "create table NFDB("
                   + "ID integer primary key autoincrement, "
                   + "USERNAME text, PASSWORD text, "
                   + "EMAIL text, NUMBERINPUT text, "
                   + "SCORE text, FRIENDS text)"; 

and second statement: 第二句话:

CREATE_INDEX_KEYTYPE = "CREATE UNIQUE INDEX idx_keytype ON tableName(USERNAME)";

And finally, in your SQLiteOpenHelper subclass implementation perform following actions: 最后,在您的SQLiteOpenHelper子类实现中,执行以下操作:

_db.execSQL(DBAdapter.DATABASE_CREATE);
_db.execSQL(DBAdapter.CREATE_INDEX_KEYTYPE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM