在数据库

时间:2016-11-04 03:10:47

标签: android database sqlite

我有包含sqlite数据库和表的示例代码,问题是当我尝试添加1个表并在我的设备中安装应用程序时,创建了数据库,但是表,实际表和新表表未创建。我已经尝试更改数据库版本和名称,但它对我不起作用。这是我的数据处理程序:

package com.AndroidDevelopmentGuru.database_new;
        import java.util.ArrayList;
        import java.util.List;
        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteOpenHelper;

    public class DatabaseHandler extends SQLiteOpenHelper {

        public DatabaseHandler(Context context, Object name,
                Object factory, int version) {
            // TODO Auto-generated constructor stub
            super(context,  DATABASE_NAME, null, DATABASE_VERSION);
        }


        String password;
        // Database Version
        private static final int DATABASE_VERSION = 5;
        // Database Name
        private static final String DATABASE_NAME = "User.db";

        // Contacts table name
        private static final String TABLE_REGISTER= "register"; 
        public static final String KEY_ID = "id";
        public static final String KEY_FIRST_NAME = " first_name";
        public static final String KEY_lAST_NAME = "last_name";
        public static final String KEY_EMAIL_ID="email_id";
        public static final String KEY_MOB_NO = "mobile_number";
        public static final String KEY_PASSWORD = "password";
        public static final String CREATE_TABLE="CREATE TABLE " + TABLE_REGISTER + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRST_NAME + " TEXT,"+KEY_lAST_NAME + " TEXT,"+KEY_EMAIL_ID+ " TEXT,"
                + KEY_MOB_NO + " TEXT," + KEY_PASSWORD + " TEXT " + ")";

        private static final String TABLE_CAR = "car";
        public static  final String KEY_CAR_ID = "car_id";
        public static final String KEY_CAR_NAME = "car_name";
        public static final String KEY_CAR_PRICE = "car_price";
        public static final String KEY_CAR_DESCP = "car_descp";
        public static final String CREATE_TABLE_CAR = "CREATE TABLE " + TABLE_CAR + "("
                + KEY_CAR_ID + "INTEGER PRIMARY KEY,"
                + KEY_CAR_NAME + "TEXT,"
                + KEY_CAR_PRICE + "INTEGER,"
                + KEY_CAR_DESCP + "TEXT," + ")";

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_TABLE);
            db.execSQL(CREATE_TABLE_CAR);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAR);

            // Create tables again
            onCreate(db);
        }

        void addregister(Registerdata registerdata)   
                // code to add the new register
        {  
            SQLiteDatabase db = this.getWritableDatabase();   
            ContentValues values = new ContentValues();    
            values.put(KEY_FIRST_NAME,registerdata.getfirstName()); // register first Name
            values.put(KEY_lAST_NAME, registerdata. getlastName() ); // register last name
            values.put(KEY_EMAIL_ID, registerdata.getEmailId());//register email id
            values.put(KEY_MOB_NO, registerdata.getMobNo());//register mobile no
            values.put(KEY_PASSWORD, registerdata.getPassword());
            // Inserting Row

            db.insert(TABLE_REGISTER, null, values);
            db.close(); // Closing database connection

        }  



        //code to get the register
        String getregister(String username){
            SQLiteDatabase db = this.getReadableDatabase();
            //String selectquery="SELECT * FROM TABLE_REGISTER";
            Cursor cursor=db.query(TABLE_REGISTER,null,  "email_id=?",new String[]{username},null, null, null, null);

            if(cursor.getCount()<1){
                cursor.close();
                return "Not Exist";
            }
            else if(cursor.getCount()>=1 && cursor.moveToFirst()){

                 password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
                cursor.close();

            }
            return password;


    }


        /*public String getDatabaseName() {
            return DATABASE_NAME;
        }

        public static String getKeyId() {
            return KEY_ID;
        }

        public static String getTableContacts() {
            return TABLE_REGISTER;
        }

        public static int getDatabaseVersion() {
            return DATABASE_VERSION;
        }*/

    }

1 个答案:

答案 0 :(得分:0)

也许你可以在TEXT之前添加一个空格

public static final String CREATE_TABLE_CAR = "CREATE TABLE " + TABLE_CAR + "("
                    + KEY_CAR_ID + "INTEGER PRIMARY KEY,"
                    + KEY_CAR_NAME + "TEXT,"
                    + KEY_CAR_PRICE + "INTEGER,"
                    + KEY_CAR_DESCP + "TEXT," + ")";

public static final String CREATE_TABLE_CAR = "CREATE TABLE " + TABLE_CAR + "("
                    + KEY_CAR_ID + " INTEGER PRIMARY KEY,"
                    + KEY_CAR_NAME + " TEXT,"
                    + KEY_CAR_PRICE + " INTEGER,"
                    + KEY_CAR_DESCP + " TEXT)";