在Android中使用SQLite数据库插入数据时出错

时间:2013-12-17 06:09:41

标签: java android android-sqlite

我想在SQLite数据库中插入10到15列,但是当我运行应用程序时,它会显示错误消息:

  

sqlite返回:错误代码= 1,msg =表new_lead没有名为add_to_contacts的列

在SQLite数据库中插入数据时获取SQLite异常。

这是我的数据库代码。

public class DataBaseHelper extends SQLiteOpenHelper
{




    public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
    {
               super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) 
    {
            _db.execSQL(DataBase_Adapter.DATABASE_CREATE_LOGIN);
           _db.execSQL(DataBase_Adapter.CREATE_NEW_LEAD_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
    {

            Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

            _db.execSQL("DROP TABLE IF EXISTS " + DataBase_Adapter.TABLE_NAME_LOGIN);
            _db.execSQL("DROP TABLE IF EXISTS " + DataBase_Adapter.TABLE_NEW_LEAD);

            onCreate(_db);
    }

}

这是我的数据库适配器代码。

public class DataBase_Adapter 
{

        //Database NAme
        static final String DATABASE_NAME = "lead_management.db";

        //Database Version
        static final int DATABASE_VERSION = 4;


        // 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  DataBase_Adapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public  DataBase_Adapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }

        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }
    // TODO Auto-generated method 
           /*************************************Table New Lead*******************************************/

            //Table name
            public static  String TABLE_NEW_LEAD="new_lead";

            //Creating New Lead Table Columns
            public static final String KEY_NEW_LEAD_ID ="id";
            public static final String KEY_ORGANIZATION_NAME="organization";
            public static final String KEY_NEW_LEAD_NAME ="name"; 
            public static final String KEY_NEW_LEAD_EMAIL ="email";
            public static final String KEY_NEW_LEAD_MOBILE="mobile";
            public static final String KEY_NEW_LEAD_Product="define_products";
            public static final String KEY_NEW_LEAD_BUDGET="budget";
            public static final String KEY_NEW_LEAD_PRIORITY="priority";
            public static final String KEY_NEW_LEAD_STATUS="status";
            public static final String KEY_NEW_LEAD_NOTES="notes";
            public static final String KEY_NEW_LEAD_REMINDER_DATE="reminder_date";
            public static final String KEY_NEW_LEAD_REMINDER_TIME="reminder_time";
            public static final String KEY_NEW_LEAD_ADDtoCONTACTS="add_to_contacts";
            public static final String KEY_NEW_LEAD_CurrentDate="current_date";


            //// SQL Statement to create a New Lead Database.


            static final String CREATE_NEW_LEAD_TABLE = "CREATE TABLE "+ TABLE_NEW_LEAD + "("
                                                    + KEY_NEW_LEAD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                                                    + KEY_ORGANIZATION_NAME + " TEXT,"
                                                    + KEY_NEW_LEAD_NAME + " TEXT, "
                                                    + KEY_NEW_LEAD_EMAIL+ " TEXT, "
                                                    + KEY_NEW_LEAD_MOBILE + " TEXT, "
                                                    + KEY_NEW_LEAD_Product + " TEXT, "
                                                    + KEY_NEW_LEAD_BUDGET + " TEXT, "
                                                    + KEY_NEW_LEAD_PRIORITY + " TEXT, "
                                                    + KEY_NEW_LEAD_STATUS + " TEXT, "
                                                    + KEY_NEW_LEAD_NOTES + " TEXT, "
                                                    + KEY_NEW_LEAD_REMINDER_DATE + " TEXT, "
                                                    + KEY_NEW_LEAD_REMINDER_TIME + " TEXT, "
                                                    + KEY_NEW_LEAD_ADDtoCONTACTS + " TEXT, "
                                                    + KEY_NEW_LEAD_CurrentDate + " TEXT "+");"; 


          public void insert_NewLead_Entry(New_Lead_BeanClass newLead_BeanClass)
          {
              SQLiteDatabase sdb = dbHelper.getWritableDatabase();

              ContentValues contentNewLead_Val=new ContentValues();

              contentNewLead_Val.put(KEY_ORGANIZATION_NAME, newLead_BeanClass.get_organization());
              contentNewLead_Val.put(KEY_NEW_LEAD_NAME, newLead_BeanClass.get_Name());
              contentNewLead_Val.put(KEY_NEW_LEAD_EMAIL, newLead_BeanClass.get_Email());
              contentNewLead_Val.put(KEY_NEW_LEAD_MOBILE, newLead_BeanClass.get_MobileNo());
              contentNewLead_Val.put(KEY_NEW_LEAD_Product, newLead_BeanClass.get_Product());
              contentNewLead_Val.put(KEY_NEW_LEAD_BUDGET, newLead_BeanClass.get_Budget());
              contentNewLead_Val.put(KEY_NEW_LEAD_PRIORITY, newLead_BeanClass.get_Priority());
              contentNewLead_Val.put(KEY_NEW_LEAD_STATUS, newLead_BeanClass.get_Status());
              contentNewLead_Val.put(KEY_NEW_LEAD_NOTES, newLead_BeanClass.get_Notes());
              contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_DATE, newLead_BeanClass.get_Reminder_Date());
              contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_TIME, newLead_BeanClass.get_Reminder_Time());
              contentNewLead_Val.put(KEY_NEW_LEAD_ADDtoCONTACTS, newLead_BeanClass.get_AddtoContact());
              contentNewLead_Val.put(KEY_NEW_LEAD_CurrentDate, newLead_BeanClass.get_CurrentDate());

              sdb.insert(TABLE_NEW_LEAD , null , contentNewLead_Val );

              //Close The Database Connection
              sdb.close();

          }

}

以下是我插入数据的活动代码。

dbHandller=new DataBase_Adapter(this);
                dbHandller=dbHandller.open();

                New_Lead_BeanClass  new_Lead_BeanClass = new New_Lead_BeanClass();

                new_Lead_BeanClass.set_organization(strOrgName);
                new_Lead_BeanClass.set_Name(strContactName);
                new_Lead_BeanClass.set_Email(strContactEmail);
                new_Lead_BeanClass.set_MobileNo(strContactMobile);
                new_Lead_BeanClass.set_Product(selectedProductItem);
                new_Lead_BeanClass.set_Budget(strBudget);
                new_Lead_BeanClass.set_Priority(selectedPriorityItem);
                new_Lead_BeanClass.set_Status(selectedStatusItem);
                new_Lead_BeanClass.set_Notes(strNotes);
                new_Lead_BeanClass.set_Reminder_Date(strDate);
                new_Lead_BeanClass.set_Reminder_Time(strTime);
                new_Lead_BeanClass.set_AddtoContact(strToggleVlaue);
                new_Lead_BeanClass.set_CurrentDate(strCurrentDate);

                dbHandller.insert_NewLead_Entry(new_Lead_BeanClass);

日志cat堆栈跟踪信息

12-17 11:28:57.703: E/Database(336): Error inserting budget=7878 reminder_time=2 : 27 organization=war add_to_contacts=true status=open email=g@g.c priority=medium current_date=17-Dec-2013 name=gt define_products=a reminder_date=3-17-2014 notes=vhbgjhngk mobile=56768
12-17 11:28:57.703: E/Database(336): android.database.sqlite.SQLiteException: table new_lead has no column named add_to_contacts: , while compiling: INSERT INTO new_lead(budget, reminder_time, organization, add_to_contacts, status, email, priority, current_date, name, define_products, reminder_date, notes, mobile) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
12-17 11:28:57.703: E/Database(336):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
12-17 11:28:57.703: E/Database(336):    at com.lead_management_project.DataBase_Adapter.insert_NewLead_Entry(DataBase_Adapter.java:245)
12-17 11:28:57.703: E/Database(336):    at com.lead_management_project.New_Lead_Activity5$9.onClick(New_Lead_Activity5.java:411)
12-17 11:28:57.703: E/Database(336):    at android.view.View.performClick(View.java:2485)
12-17 11:28:57.703: E/Database(336):    at android.view.View$PerformClick.run(View.java:9080)
12-17 11:28:57.703: E/Database(336):    at android.os.Handler.handleCallback(Handler.java:587)
12-17 11:28:57.703: E/Database(336):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 11:28:57.703: E/Database(336):    at android.os.Looper.loop(Looper.java:123)
12-17 11:28:57.703: E/Database(336):    at android.app.ActivityThread.main(ActivityThread.java:3683)
12-17 11:28:57.703: E/Database(336):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 11:28:57.703: E/Database(336):    at java.lang.reflect.Method.invoke(Method.java:507)
12-17 11:28:57.703: E/Database(336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 11:28:57.703: E/Database(336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 11:28:57.703: E/Database(336):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您的数据库未正确创建。您尚未在课程中放置onCreate(SQLiteDatabase obj)方法。

检查this project

见Vogella的this tutorial

这是可能的错误。

答案 1 :(得分:1)

您应该在助手类的onCreate()中包含此方法

  static final String CREATE_NEW_LEAD_TABLE = "CREATE TABLE "+ TABLE_NEW_LEAD + "("
                                            + KEY_NEW_LEAD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                                            + KEY_ORGANIZATION_NAME + " TEXT,"
                                            + KEY_NEW_LEAD_NAME + " TEXT, "
                                            + KEY_NEW_LEAD_EMAIL+ " TEXT, "
                                            + KEY_NEW_LEAD_MOBILE + " TEXT, "
                                            + KEY_NEW_LEAD_Product + " TEXT, "
                                            + KEY_NEW_LEAD_BUDGET + " TEXT, "
                                            + KEY_NEW_LEAD_PRIORITY + " TEXT, "
                                            + KEY_NEW_LEAD_STATUS + " TEXT, "
                                            + KEY_NEW_LEAD_NOTES + " TEXT, "
                                            + KEY_NEW_LEAD_REMINDER_DATE + " TEXT, "
                                            + KEY_NEW_LEAD_REMINDER_TIME + " TEXT, "
                                            + KEY_NEW_LEAD_ADDtoCONTACTS + " TEXT, "
                                            + KEY_NEW_LEAD_CurrentDate + " TEXT "+");"; 

看看这个教程

Sqlite Integartion

答案 2 :(得分:0)

如果这是您的全部代码,

  • 您没有在任何地方调用CREATE_NEW_LEAD_TABLE命令 代码,检查你是否从SQLiteOpenHelper扩展了类 实施了onCreate(SQLIteDatabase db)等方法 和onUpgrade()方法
  • 如果是,您需要检查是否已经从已经升级 现有数据库
  • 如果是,请尝试更改DB_Version变量

,请尝试以下代码

public class dbHandler extends SQLiteOpenHelper{
static String DB_NAME="mapsDb";
static int DB_VERSION=1;
String TAB_NAME="usertable";
String FILED_ID="id";
String FILED_DRIVERNAME="driver_name";
String FILED_CARNUMBER="cno";
String FILED_CARMODEL="cmodel";
String MARKER_TAB="markerTable";
String FILED_DRIVERID="id";
String FILED_LAT="latitude";
String FILED_LON="longtitude";
public static Context con;
    public dbHandler(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
        this.con=context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE="CREATE TABLE "+TAB_NAME+" ("+FILED_ID+" TEXT PRIMARY KEY,"+FILED_DRIVERNAME+" TEXT,"+FILED_CARNUMBER+" TEXT,"+FILED_CARMODEL+" TEXT);";
        String CREATE_MARKE_TABLE="CREATE TABLE "+MARKER_TAB+" ("+FILED_DRIVERID+" TEXT,"+FILED_LAT+" TEXT,"+FILED_LON+" TEXT);";
        db.execSQL(CREATE_TABLE);
        db.execSQL(CREATE_MARKE_TABLE);
        //db.update(table, values, whereClause, whereArgs)
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TAB_NAME);
        db.execSQL("DROP TABLE IF EXISTS "+MARKER_TAB);
        onCreate(db);

    }
    public void insertDriverDetails(String id, String name,
            String cno, String mno) {
        // TODO Auto-generated method stub
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(FILED_ID, id);
        cv.put(FILED_DRIVERNAME, name);
        cv.put(FILED_CARNUMBER, cno);
        cv.put(FILED_CARMODEL, mno);
        try{
        db.insert(TAB_NAME, null, cv);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        db.close();



    }
    public void updateMarkerForId(String id, String longt, String lat) {
        // TODO Auto-generated method stub
        SQLiteDatabase db=this.getWritableDatabase();
        String UPDATE_QUERY="UPADATE "+MARKER_TAB+" SET "+FILED_LON+"=\""+longt+"\","+FILED_LAT+"\""+lat+"\" WHERE "+FILED_DRIVERID+"=\""+id+"\";";
        db.execSQL(UPDATE_QUERY);
//      db.raw
//      db.close();
    }

}