无法创建数据库

时间:2016-06-25 07:27:15

标签: java android sqlite

这些是我运行应用程序时重复3或4次的logcat错误。我附上了数据库助手:

Error inserting time_type=3 proto_blob=[B@42439540  sync_state_mod_time_millis=1466838685054 context_id=e7029963-e329-4b1b-bb6e-24bb57e0ba04 end_time=1466837904524 module_id=com.google.android.contextmanager.module.ScreenModule start_time=1466837558811 sync_state=0 context_family=7 context_name=7 version=1
  android.database.sqlite.SQLiteConstraintException: column context_id is not unique (code 19)
  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
  at bsv.a(SourceFile:445)
  at bsv.b(SourceFile:420)
  at bsv.a(SourceFile:370)
  at bsv.a(SourceFile:147)
  at bsc.a(SourceFile:157)
  at bjb.a(SourceFile:64)
  at bix.run(SourceFile:52)
  at biv.a(SourceFile:258)
  at biv.handleMessage(SourceFile:251)
  at jcq.run(SourceFile:141)
  at jcy.run(SourceFile:438)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at jhi.run(SourceFile:17)
  at java.lang.Thread.run(Thread.java:841)

这是Databasehelper类:

public class databasehelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="USMS.db";
    public static final String TABLE_NAME="B_LIST";
    public static final String COL1="ID";
    public static final String COL2="NAME";
    public static final String COL3="NUMBER";


    public databasehelper(Context context) {
        super(context, DATABASE_NAME, null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create Table "+TABLE_NAME+ "(ID INTEGER PRIMARY KEY       AUTOINCREMENT, name text, number text);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public boolean InsertData(String name, String number) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values =new ContentValues();
        values.put(COL2,name);
        values.put(COL3,number);
        Long result = db.insert(TABLE_NAME, null ,values);
        if(result==-1)
            return false;
        else
            return  true;
    }
}

这是我的主要活动类:

public class MainActivity extends AppCompatActivity {
    databasehelper myDb;
    EditText editname,editnumber;
    Button bt1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb=new databasehelper(this);
        editname = (EditText)findViewById(R.id.editText);
        editnumber = (EditText)findViewById(R.id.editText2);
        bt1 = (Button) findViewById(R.id.button);
    }

    public void adddate(){
        bt1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                boolean inserted = myDb.InsertData(editname.getText().toString(),editnumber.getText().toString());
                if (inserted=true)
                    Toast.makeText(MainActivity.this,"data inserted",Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this,"not inserted",Toast.LENGTH_LONG).show();
             }
         });
    }
}

我希望有人可以帮助我,提前谢谢!

0 个答案:

没有答案