Android 3.1 SQLite数据库不会创建表并向“数据库设备浏览器”监视器插入字段吗?

时间:2018-11-12 03:46:57

标签: android database sqlite device

我正在尝试通过android 3.1创建SQLite数据库。但是它不能作为下面的代码工作。请帮助谢谢。

主要活动.class

public class MainActivity extends AppCompatActivity { DatabaseHelper myDb; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);  // this is contact, this is new extension of DatabaseHelper

    } 
} 

DatabaseHelper.class

public class DatabaseHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "Iters.db";
    public static final String TABLE_NAME = "iters_table"; //called onCreate method
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";
    public static final String COL_4 = "MARKS";


    public DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase(); 
    }

    @Override

    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                "NAME TEXT," +
                "SURNAME TEXT," +
                "MARKS INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
        onCreate(db);
    } 
 }

1 个答案:

答案 0 :(得分:0)

似乎您的问题可能是您在命名表 TABLE_NAME 而不是 iters_table

您可以使用:-

对此进行更正。
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY," +
                "NAME TEXT," +
                "SURNAME TEXT," +
                "MARKS INTEGER)");
  • 注意,我也删除了 AUTOINCREMENT 关键字,因为您很可能不需要它(ID列仍会自动生成,然后是1,然后是2 3等(可能))为:-