Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite Android中的负id值_Android_Sqlite - Fatal编程技术网

SQLite Android中的负id值

SQLite Android中的负id值,android,sqlite,Android,Sqlite,在主活动中,我创建了数据库 class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { super(context, "myDB", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("

在主活动中,我创建了数据库

class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {

               super(context, "myDB", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
          db.execSQL("create table mytable ("
                  + "id integer primary key autoincrement," 
                  + "name text," 
                  + "obId text," 
                  + "latit text,"
                  + "long text" + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
      }
在第二个活动中,我调用数据库类

MainActivity myActivity = new MainActivity();
dbHelper = myActivity.new DBHelper(myActivity);
并插入数据

ContentValues cv = new ContentValues();
db = openOrCreateDatabase("myDB.db", SQLiteDatabase.OPEN_READWRITE, null);
cv.put("name", textViewName.getText().toString());
long rowID = db.insert("mytable", null, cv);
db.insert("mytable", null, cv);
dbHelper.close();

输出“rowID”为“-1”。我不明白为什么会发生这种情况

在数据库助手上使用
getWritableDatabase()
,而不是在
上下文上使用
openOrCreateDatabase()
来获取数据库引用。您正在两个不同的数据库上操作,另一个数据库没有您要插入的表

此外,您将插入相同的数据两次

如果我使用db=dbHelper.getWritableDatabase();我将在android.database.sqlite.SQLiteOpenHelper.getwriteabledatabase(SQLiteOpenHelper.ja)的android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)中记录java.lang.NullPointerException‌​va:118)在com.ex.test.AddPoint.writePointsToDatabase(Add.java:264)在com.ex.test.AddPoint$3$2.onClick(Add.java:238)上

这是因为您在此处向帮助者传递了无效的
上下文

MainActivity myActivity = new MainActivity();
dbHelper = myActivity.new DBHelper(myActivity);
不能使用
新建
实例化活动。在活动中,使用
引用有效的
上下文

您正在两个不同的数据库上操作

这就是“myDB”

与中的“myDB.db”相反


如果我使用
db=dbHelper.getWritableDatabase(),那么这是最好的我将在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)的android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)的com.ex.test.AddPoint.writePointsToDatabase(Add.java:264)的com.ex.test.AddPoint$3$2.onClick(Add.java:238)的日志中记录
java.lang.NullPointerException
264行是
db=dbHelper.getWritableDatabase()这是因为您向sqlite助手构造函数提供了无效的
上下文。
           super(context, "myDB", null, 1);
db = openOrCreateDatabase("myDB.db", SQLiteDatabase.OPEN_READWRITE, null);