mysql 多个 android,如何在Android Studio数据库中创建多个表

Sorry to bother you all, but sometimes the help is what I have to ask for. I'm new to Android development, and currently I'm following tutorial for android database creation on youtube at channel ProgrammingKnowledge, however, it works only for one table, while I need to have 3 tables in the database, and can't get my way around it. Any help would be appreciated. Thanks in advance!

This is the code I currently have.

public class DatabaseHelper extends SQLiteOpenHelper {

//database name declaration

public static final String DATABASE_NAME = "museum.db";

public static final String TABLE_NAME = "exponent_table";

public static final String COL_1 = "ID";

public static final String COL_2 = "TITLE";

public static final String COL_3 = "STORY";

public static final String COL_4 = "AUTHOR";

public static final String COL_5 = "DATE";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, 1);

SQLiteDatabase db= this.getWritableDatabase(); //will create database and table, just for checking, will be replaced

}

@Override

public void onCreate(SQLiteDatabase db) {

//creating tables ???

db.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, STORY TEXT, AUTHOR TEXT, DATE STRING)");

}

@Override

public void onUpgrade(SQLiteDatabase db, int i, int i1) {

db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);

onCreate(db);

}

}

I have seen something similar on the link:

Multi-table database SQLite android

But still do not understand.

解决方案

this is very good tutorial..read it

you can create multiple tables like this

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, STORY TEXT, AUTHOR TEXT, DATE STRING)");

db.execSQL("create table "+TABLE_NAME1+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, Table1Field TEXT)");

}

@Override

public void onUpgrade(SQLiteDatabase db, int i, int i1) {

db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);

db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME1);

onCreate(db);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值