升级我的数据库

时间:2015-07-20 14:16:27

标签: android database sqlite

我的应用程序上有一个数据库,但我有两个问题:

  • 数据库名称与我写的不一致:

错误:

sqlite returned: error code = 1, msg = table demandes has no column named dateEnvoie, db=/data/data/(package)/databases/google_analytics_v4.db

我的代码:

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "Solutis";

// Contacts table name
private static final String TABLE_DEMANDES = "demandes";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_XML = "xml";
private static final String KEY_STATUTENVOIE = "statutEnvoie";
private static final String KEY_DATEENVOIE = "dateEnvoie";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

错误说我的数据库名称是= google_analytics_v4.db,但在我的代码DATABASE_NAME =" Solutis"。

  • 我的第二个问题是数据库升级,我做了一些更改(我添加了一个列)

我必须称之为:

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEMANDES);

    // Create tables again
    onCreate(db);
}
但是该怎么办?我在片段中工作,我传递了什么参数?

CODE

    public class DatabaseHandler extends SQLiteOpenHelper {

        // All Static variables
        // Database Version
        private static final int DATABASE_VERSION = 1;

        // Database Name
        private static final String DATABASE_NAME = "Solutis";

        // Contacts table name
        private static final String TABLE_DEMANDES = "demandes";

        // Contacts Table Columns names
        private static final String KEY_ID = "id";
        private static final String KEY_XML = "xml";
        private static final String KEY_STATUTENVOIE = "statutEnvoie";
        private static final String KEY_DATEENVOIE = "dateEnvoie";

        public DatabaseHandler(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        // Creating Tables
        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_DEMANDES_TABLE = "CREATE TABLE " + TABLE_DEMANDES + "("
                    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_XML + " TEXT,"
                    + KEY_STATUTENVOIE + " INTEGER" + KEY_DATEENVOIE + " DATETIME" + ")";
            db.execSQL(CREATE_DEMANDES_TABLE);
        }

        // Upgrading database
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEMANDES);

            // Create tables again
            onCreate(db);
        }
(functons...)
    }
public class DemandeGratuite extends android.support.v4.app.Fragment {
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //placer ici le code pour connaitre la densite et la resolution de lecran
        View view = inflater.inflate(R.layout.demande_gratuite, container, false);
        valider.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                            db = new DatabaseHandler(getActivity());

                            xml = "...";

                            Demande demande = db.addDemande(new Demande(xml, 0, ""));

                            demandeId = demande.getID();

                            db.updateIdContactClient(demandeId);

                    }
                }
        );


        return view;
    }

2 个答案:

答案 0 :(得分:1)

我注意到的一个问题是您创建的字符串CREATE_DEMANDES_TABLE格式不正确,不能用作SQL语句。您错过了KEY_DATEENVOIE之前的逗号,因此它未被识别为数据库中的表。将字符串更改为:

String CREATE_DEMANDES_TABLE = "CREATE TABLE " + TABLE_DEMANDES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_XML + " TEXT,"
                + KEY_STATUTENVOIE + " INTEGER," + KEY_DATEENVOIE + " DATETIME" + ")";

答案 1 :(得分:0)

听起来数据库没有根据您的更改进行更新。为了获得要调用的onUpgrade方法,并进行更改,您需要更新数据库版本号。将DATABASE_VERSION更改为2