[英]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.
为此,我需要使用数据库,我需要为餐厅建议的产品构建表,为不同的Category , Users表和Reservations表构建表。 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? 如何构建数据库,以便每个Activity在首次创建后都能对其进行访问? **Edit:**I have already created the "CREATE_TABLE_NAME" that it is the String that sends to the SQL.
**编辑:**我已经创建了“ CREATE_TABLE_NAME”,它是发送到SQL的字符串。
I am having troubles to implement the onCreate of the DataBase file in the Main class. 我在实现Main类中的数据库文件的onCreate时遇到麻烦。 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 . 在此处查看API文档。 The example gives you a nice class design that you can follow.
该示例为您提供了不错的类设计,可以遵循。 A short summary:
简短摘要:
SQLiteOpenHelper
SQLiteOpenHelper
的子类 onCreate(SQLiteDatabase db)
onCreate(SQLiteDatabase db)
sql create statement
on the db
db
上执行sql create statement
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. 如果您创建新对象,则Android运行时不会创建完整的新数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.