SQLite数据库错误

时间:2014-11-18 21:01:01

标签: android android-sqlite

我正在尝试创建一个Android应用程序来将sms备份到sqlite数据库。我在SQLiteOpenHelper类中专门创建数据库时遇到错误。

以下是主要活动中的代码

public class MainActivity extends Activity {

    SimpleCursorAdapter adapter;
    private smsDatabase db1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);





    }

    public void Backup(View view) {

        Uri convo = Uri.parse("content://sms");
        String[] reqCol = new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"};
        ContentResolver contentResolver = getContentResolver();
        Cursor c = contentResolver.query(convo, null, null, null, "date asc");
        SMSList<SMS> smsList = new SMSList<SMS>();
        String count = Integer.toString(c.getCount());
        Log.d("Count",count);
        Context ctx = getApplicationContext();
        db1 = new smsDatabase(ctx);
        db1.open();
        while (c.moveToNext()) {
            SMS txt = new SMS();

            long dateLong = c.getLong(c.getColumnIndex("date"));
            Integer id1 = c.getInt(c.getColumnIndex("_id"));
            String body = c.getString(c.getColumnIndex("body"));
            Integer type1 = c.getInt(c.getColumnIndex("type"));
            String address1 = c.getString(c.getColumnIndex("address"));
            String read1 = c.getString(c.getColumnIndex("read"));
            String seen1 = c.getString(c.getColumnIndex("seen"));
            String subject1 = c.getString(c.getColumnIndex("subject"));
            smsList.add(txt);

            db1.insertSMS(txt);

        }
    }

这是数据库类

package com.swavey.testing;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.security.KeyChain;

import java.util.Date;

/**
 * Created by Adrian on 11/5/2014.
 */
public class smsDatabase  {

    private static final String KEY_ID = "_id";
    private static final int COLUMN_ID =0;

    // database info
    public static final String DATABASE_NAME = "texts";
    public static final String DATABASE_TABLE = "mainTable";
    public static final int DATABASE_VERSION = (int) new Date().getTime()/1000;

    // list of fields
    private static final String KEY_ADDRESS = "address";
    private static final String KEY_BODY = "body";
    private static final String KEY_DATE = "date";
    private static final String KEY_DATESENT = "dateSent";
    private static final String KEY_READ = "read";
    private static final String KEY_SUBJECT = "subject";
    private static final String KEY_THREADID = "thread_id";
    private static final String KEY_PERSON = "person";
    private static final String KEY_PROTOCOL = "protocol";
    private static final String KEY_TYPE = "type";
    private static final String KEY_SERVICECENTER = "serviceCenter";

    //list of field numbers
    private static final int COL_ADDRESS = 1;
    private static final int COL_BODY = 2;
    private static final int COL_DATE = 3;
    private static final int COL_DATESENT = 4;
    private static final int COL_READ = 5;
    private static final int COL_SUBJECT = 6;
    private static final int COL_THREADID = 7;
    private static final int COL_PERSON = 8;
    private static final int COL_PROTOCOL = 9;
    private static final int COL_TYPE = 10;
    private static final int COL_SERVICECENTER = 11;

    //create string array of all fields;
    public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADDRESS, KEY_BODY, KEY_DATE,
        KEY_DATESENT, KEY_PERSON, KEY_PROTOCOL,KEY_READ, KEY_SERVICECENTER, KEY_SUBJECT,
        KEY_THREADID, KEY_TYPE};


    private static final String DATABASE_CREATE_SQL = "create table " + DATABASE_TABLE
            + " (" + KEY_ID + " integer primary key autoincrement, "
            +KEY_ADDRESS + " text not null, "
            +KEY_BODY + " text not null, "
            +KEY_DATE + " text not null, "
            +KEY_DATE + " text not null, "
            +KEY_PERSON+ " text not null, "
            +KEY_PROTOCOL+ " text not null, "
            +KEY_READ+ " text not null, "
            +KEY_PERSON + " text not null, "
            +KEY_PROTOCOL+ " text not null, "
            +KEY_READ+ " text not null, "
            +KEY_SERVICECENTER+ " text not null, "
            +KEY_SUBJECT+ " text not null, "
            +KEY_THREADID+ " text not null, "
            +KEY_TYPE+ " text not null"
            + ");";

    private final Context context;

    private DatabaseHelper dbHelper;
    private SQLiteDatabase db;

    public smsDatabase (Context cxt) {
        this.context = cxt;
        dbHelper = new DatabaseHelper(context);
    }

    //open database
    public smsDatabase open() {
        db = dbHelper.getWritableDatabase();
        return this;
    }
    //close database
    public void close() {
        dbHelper.close();
    }

    // insert sms into table
    public long insertSMS (SMS sms) {
        ContentValues iv = new ContentValues();
        iv.put(KEY_ADDRESS, sms.getAddress());
        iv.put(KEY_BODY, sms.getBody());
        iv.put(KEY_DATE, sms.getDate());
        iv.put(KEY_DATESENT, sms.getDateSent());
        iv.put(KEY_PERSON, sms.getPerson());
        iv.put(KEY_PROTOCOL, sms.getProtocol());
        iv.put(KEY_READ, sms.getRead());
        iv.put(KEY_SERVICECENTER, sms.getServiceCenter());
        iv.put(KEY_SUBJECT, sms.getSubject());
        iv.put(KEY_THREADID, sms.getThread_id());
        iv.put(KEY_TYPE, sms.getType());

        return db.insert(DATABASE_TABLE, null,iv);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);
        }


        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            _db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE);
            onCreate(_db);
        }
    }
}

这是抛出的日志错误

java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3591)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16966)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Version must be >= 1, was -994802
        at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:100)
        at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:78)
        at com.swavey.testing.smsDatabase$DatabaseHelper.<init>(smsDatabase.java:115)
        at com.swavey.testing.smsDatabase.<init>(smsDatabase.java:82)
        at com.swavey.testing.MainActivity.Backup(MainActivity.java:44)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

  

引起:java.lang.IllegalArgumentException:版本必须是&gt; = 1,   是-994802

public static final int DATABASE_VERSION = (int) new Date().getTime()/1000;

您的数据库版本应为static int,例如 1 2 (等)。每当您发布应用程序更新并且数据库结构因更新而发生更改时,您可以在此版本号中添加一个以触发onUpgrade运行。