简体   繁体   English

Android中SQLlite编程错误

[英]Error in sqllite programming in android

I am designing an app for messaging.i am using a sqlite database to store users information but i am getting this error logcat. 我正在设计一个用于Messenger的应用程序。我正在使用一个sqlite数据库来存储用户信息,但是却收到了此错误logcat。

05-19 22:24:00.211: E/SQLiteLog(8815): (1) no such table: pendingintents
05-19 22:24:00.251: E/SQLiteDatabase(8815): Error inserting message=aaaaaaaas receivername=Abc minutes=23 _id=-1092327224 seconds=28 month=4 year=2013 frequency=15 mins day=19 hour=22 numbertosend=(880) 037-6666
05-19 22:24:00.251: E/SQLiteDatabase(8815): android.database.sqlite.SQLiteException: no such table: pendingintents (code 1): , while compiling: INSERT INTO pendingintents(message,receivername,minutes,_id,seconds,month,year,frequency,day,hour,numbertosend) VALUES (?,?,?,?,?,?,?,?,?,?,?)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.xxxx.DatabaseCreator.InitializePendingIntents(DatabaseCreator.java:64)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.xxxx.Scheduler.addToDatabase(Scheduler.java:314)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.xxxx.Scheduler.access$20(Scheduler.java:311)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.xxxx.Scheduler$4.onClick(Scheduler.java:188)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.view.View.performClick(View.java:4204)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.view.View$PerformClick.run(View.java:17355)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.os.Handler.handleCallback(Handler.java:725)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.os.Looper.loop(Looper.java:137)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at java.lang.reflect.Method.invoke(Method.java:511)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-19 22:24:00.251: E/SQLiteDatabase(8815):     at dalvik.system.NativeStart.main(Native Method)
05-19 22:24:00.251: E/SQLiteLog(8815): (1) no such table: pendingintents

pendingintents is the name i am giving to my table. 我给表指定了“ pendingintents”名称。

here is my sqlite helper class 这是我的sqlite助手类

public class SQLLiteOpenHelper extends SQLiteOpenHelper {

 public static final String TABLE_PENDINGINTENT = "pendingintents";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_HOUR = "hour";
  public static final String COLUMN_MINUTES = "minutes";
  public static final String COLUMN_SECONDS = "seconds";
  public static final String COLUMN_YEAR = "year";
  public static final String COLUMN_MONTH = "month";
  public static final String COLUMN_DAY = "day";
  public static final String COLUMN_FREQUENCY = "frequency";
  public static final String COLUMN_NUMBERTOSEND = "numbertosend";
  public static final String COLUMN_RECEIVERNAME = "receivername";
  public static final String COLUMN_MESSAGE = "message";
      private static final String DATABASE_NAME = "PendingIntentDatabase.db";
      private static final int DATABASE_VERSION = 1;


      private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_PENDINGINTENT + "(" + COLUMN_ID + " INTEGER PRIMARY KEY , " + COLUMN_HOUR +" INTEGER, " + COLUMN_MINUTES + " INTEGER, " + COLUMN_SECONDS + " INTEGER, " + COLUMN_YEAR + " INTEGER, " + COLUMN_MONTH + " INTEGER, " + COLUMN_DAY + " INTEGER, " + COLUMN_FREQUENCY + " String, " + COLUMN_NUMBERTOSEND + " String, " + COLUMN_RECEIVERNAME + " String, " + COLUMN_MESSAGE + " String " + ");";

public SQLLiteOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    arg0.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PENDINGINTENT);
    onCreate(db);
}

} }

the database insertion is done here' 数据库插入在这里完成”

              public ValueInsertion InitializePendingIntents(int id, int hour, int mins, int secs, int year, int month, int day, String frequency, String number, String name, String message )
  {
      ContentValues val= new ContentValues();
      val.put(SQLLiteOpenHelper.COLUMN_ID, id);
      val.put(SQLLiteOpenHelper.COLUMN_HOUR, hour);
      val.put(SQLLiteOpenHelper.COLUMN_MINUTES, mins);
      val.put(SQLLiteOpenHelper.COLUMN_SECONDS, secs);
      val.put(SQLLiteOpenHelper.COLUMN_YEAR, year);
      val.put(SQLLiteOpenHelper.COLUMN_MONTH, month);
      val.put(SQLLiteOpenHelper.COLUMN_DAY, day);
      val.put(SQLLiteOpenHelper.COLUMN_FREQUENCY, frequency);
      val.put(SQLLiteOpenHelper.COLUMN_NUMBERTOSEND, number);
      val.put(SQLLiteOpenHelper.COLUMN_RECEIVERNAME, name);
      val.put(SQLLiteOpenHelper.COLUMN_MESSAGE, message);

      database.insert(SQLLiteOpenHelper.TABLE_PENDINGINTENT, null,val);

      Cursor cursor = database.query(SQLLiteOpenHelper.TABLE_PENDINGINTENT,
                allColumns, SQLLiteOpenHelper.COLUMN_ID + " = " + id, null,
                null, null, null);

      cursor.moveToFirst();
      ValueInsertion newPendingIntent = cursorForPI(cursor);
      cursor.close();
      return newPendingIntent;
  }    

You also need to ensure execution of the sql statement that creates your table: 您还需要确保执行创建表的sql语句:

DATABASE_CREATE = "CREATE TABLE " + TABLE_PENDINGINTENT + "(" + COLUMN_ID + " INTEGER PRIMARY KEY , " + COLUMN_HOUR +" INTEGER, " + COLUMN_MINUTES + " INTEGER, " + COLUMN_SECONDS + " INTEGER, " + COLUMN_YEAR + " INTEGER, " + COLUMN_MONTH + " INTEGER, " + COLUMN_DAY + " INTEGER, " + COLUMN_FREQUENCY + " String, " + COLUMN_NUMBERTOSEND + " String, " + COLUMN_RECEIVERNAME + " String, " + COLUMN_MESSAGE + " String " + ");";

It's very clear by the following statement: 下面的语句很清楚:

05-19 22:24:00.211: E/SQLiteLog(8815): (1) no such table: pendingintents

that the table is not created - for whatever reason. 该表未创建-出于任何原因。 To clarify, if upgrade is getting executed, the table is dropped - you have no recreate statement afterwards... 要澄清的是,如果要执行升级,则会删除该表-之后便没有recreate语句...

Point of order. 程序点。 You name the string that creates your table DATABASE_CREATE . 您命名创建 DATABASE_CREATE的字符串。 It should be create_table_pendingintents , or something that is not as misleading as DATABASE_CREATE 它应该是create_table_pendingintents ,或者不像DATABASE_CREATE那样容易引起误解

EDIT 编辑

If you are experiencing an error during sql execution, try wrapping your code in an error trap - recommended anytime you perform db operations: 如果您在sql执行期间遇到错误,请尝试将代码包装在错误陷阱中-建议您在执行数据库操作时随时使用:

try{
    db.execSQL(DATABASE_CREATE_MY_TABLE);                                   
}catch(SQLiteException e){
   Log.v(CLASS,"Error: [{"+CLASS+"}{database}[onCreate] Exception: "+e.getMessage());
}
    private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_PENDINGINTENT + "(" + COLUMN_ID + " INTEGER PRIMARY KEY , " + COLUMN_HOUR +" INTEGER, " + COLUMN_MINUTES + " INTEGER, " + COLUMN_SECONDS + " INTEGER, " + COLUMN_YEAR + " INTEGER, " + COLUMN_MONTH + " INTEGER, " + COLUMN_DAY + " INTEGER, " + COLUMN_FREQUENCY + " String, " + COLUMN_NUMBERTOSEND + " String, " + COLUMN_RECEIVERNAME + " String, " + COLUMN_MESSAGE + " String " + ");";

    public SQLLiteOpenHelper(Context context, String name,CursorFactory factory, intversion) {
            super(context, DATABASE_NAME, factory, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
        db.execSQL(DATABASE_CREATE );
   }

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

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