Android Studio:已创建数据库,但未创建表

时间:2019-05-15 01:56:10

标签: android android-sqlite

我正在尝试使用Android Studio创建一个简单的数据库文件。我已经写了这样的代码,没有显示错误。我检查了data/data/com.example.app/databases文件,共有三个文件:Student.dbStudent.db-shmStudent.db-wal,但没有Student.db-journal像其中的教程。而且,当我尝试使用数据库浏览器打开Student.db时,不会创建任何表。

我在“创建表”部分中尝试了多种语法,但是没有用。

public class Database extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    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 Database(Context context) {
       super(context, DATABASE_NAME, null, 1);
    }

    @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 oldVersion, int newVersion) {
       db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
       onCreate(db);
    }

}

活动将会

public class MainActivity extends AppCompatActivity {

    Database myDb;

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

        myDb = new Database(this);

    }
}

5 个答案:

答案 0 :(得分:0)

查询语法例如:

 db.execSQL(" CREATE TABLE " + TABLENAME + " (" +
                KEY_STUID + " TEXT PRIMARY KEY, " +
                KEY_SUB1 + " TEXT NOT NULL, " +
                KEY_SUB2 + " TEXT NOT NULL, " +
                KEY_SUB3 + " TEXT NOT NULL, " +
                KEY_MARKS1 + " INTEGER NOT NULL, " +
                KEY_MARKS2 + " INTEGER NOT NULL, " +
                KEY_MARKS3 + " INTEGER NOT NULL);"
        );

答案 1 :(得分:0)

如果您是在首次运行应用程序时创建数据库而未创建表,则该数据库将不存在,因为每当新安装应用程序时都会调用onCreate()。

您可以通过这样更改数据库版本来调用onUpgrade()中的onCreate()方法:

// Database Version
private static final int DATABASE_VERSION = 2;

public Database(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
升级应用程序时会调用

onUpgrade(),当数据库版本不同时,会启动 AND

希望它可以为您提供帮助。

答案 2 :(得分:0)

创建一个扩展SQLiteOpenHelper并覆盖onCreate()和onUpgrade()方法的帮助器类

    private static class MyHelper extends SQLiteOpenHelper {
    public MyHelper(Context context, String name, CursorFactory factory, int version) 
    {
        super(context, name, factory, version);
    }

@Override
public void onCreate(SQLiteDatabase db) {
   db.execSQL(" CREATE TABLE " + TABLENAME + " (" +
            id+ " INTEGER PRIMARY KEY," +
            COL_2+ " TEXT," +
            COL_3+ " TEXT," +          
            COL_4+ " INTEGER);"
    );
}

答案 3 :(得分:0)

嘿@ trpn39试试这个

public class DatabaseHandler extends SQLiteOpenHelper  {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "test.db";
public static final String TABLE_NAME = "userdata_table";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_COUNTRY = "country";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_TABLE = " CREATE TABLE IF NOT EXISTS " + TABLENAME + " (" +
            KEY_STUID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_SUB1 + " TEXT NOT NULL," +
            KEY_SUB2 + " TEXT NOT NULL," +
            KEY_SUB3 + " TEXT NOT NULL," +
            KEY_MARKS1 + " INTEGER NOT NULL," +
            KEY_MARKS2 + " INTEGER NOT NULL," +
            KEY_MARKS3 + " INTEGER NOT NULL"
            + ")";

      db.execSQL(CREATE_TABLE);
}

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

开始任何活动

public class ExampleActivity extends AppCompatActivity {

DatabaseHandler databaseHandler;


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

   databaseHandler = new DataBaseHandler(this);

   //create method to store
   storeData();
}

 public void store(){

 //you need to implement getwritable database here
 ContentValues contentValues = new ContentValues();
                            SQLiteDatabase db = 
     databaseHandler.getWritableDatabase();   

 contentValues.put(SarathCityLocalDatabase.SUB_CATEGORY_LIST_ID, id);
                            contentValues.put(databaseHandler.COLUMN_ID, id);
                            contentValues.put(databaseHandler.COLUMN_NAME, name);
                            contentValues.put(databaseHandler.COLUMN_COUNTRY, county);
                            contentValues.put(databaseHandler.LOGO, logo);  

  //insert data in table

 db.insert(SarathCityLocalDatabase.public class DatabaseHandler extends 
SQLiteOpenHelper  {

 public static final int DATABASE_VERSION = 1;
 public static final String DATABASE_NAME = "test.db";
 public static final String TABLE_NAME = "userdata_table";
 public static final String COLUMN_ID = "id";
 public static final String COLUMN_NAME = "name";
 public static final String COLUMN_COUNTRY = "country";


 public DatabaseHandler(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {

 String CREATE_TABLE, null, 
   contentValues);
   }

 }

尝试此操作后,请告诉我

答案 4 :(得分:0)

结果证明代码没有错。我必须将所有三个文件Student.db,Student.db-shm,Student.db-wal放在一个文件夹中,以便使用DB Browser打开它。谢谢大家的帮助,我学到了很多:D