简体   繁体   中英

Using DataBase in android studio, how do I create the DataBase? how do I use it in different Activities?

I am only at the beginning of my project and I am stack! This is a school project, I need to create an app that would be able (theoretically) to order from restaurants from far distance, and by that to make it easy to order and pay. For that I need to use Database, I need to build table for Products that the restaurant is suggesting, Table for the different Categories , Users table and Reservations table. these are the columns in every table:

    //product table columns
public static final String PRODUCT_KEY_ID = "id";
public static final String PRODUCT_KEY_NAME = "name";
public static final String PRODUCT_KEY_CATEGORY = "category";
public static final String PRODUCT_KEY_PRICE= "price";
public static final String PRODUCT_KEY_QUANTITY= "quantity";

//category table columns
public static final String CATEGORY_KEY_ID = "id";
public static final String CATEGORY_KEY_NAME = "name";

//users table columns
public static final String USERS_KEY_ID = "id";
public static final String USERS_KEY_NAME = "name";
public static final String USERS_KEY_PASSWORD = "password";
public static final String USERS_KEY_MAIL = "mail";

//reservation table columns
public static final String RESERVATION_KEY_ID = "id";
public static final String RESERVATION_KEY_USERID = "user";
public static final String RESERVATION_KEY_PRICE = "price";
public static final String RESERVATION_KEY_PAID = "is_it_paid";

How can I build my Database so that every Activity will be able to access it after first created? **Edit:**I have already created the "CREATE_TABLE_NAME" that it is the String that sends to the SQL.

I am having troubles to implement the onCreate of the DataBase file in the Main class. also I need to save my data somehow so I will have access to it from another activity (update the Database and read from it).

thanks alot and sorry for the long question. :)

Take a look at the API docs here . The example gives you a nice class design that you can follow. A short summary:

  • make your class a subclass of SQLiteOpenHelper
  • override onCreate(SQLiteDatabase db)
  • perform your sql create statement on the db

You can access it everywhere with your context from eg a fragment or activities:

// Create an instance of your SQLiteOpenHelper Subclass (in this case YourDB)
YourDB yourDB = new YourDB(this); // the parameter here is a context. (see the API example) 
SQLiteDatabase database = yourDB.getReadableDatabase(); // or: yourDB.getWriteableDatabse() for insert/delete/ update operations.
// do your stuff with the database
// ....
// and remember to close them:
yourDB.close();
database.close();

The Android runtime won't create a complete new database if you create a new object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM