简体   繁体   English

在Android Studio中使用数据库,如何创建数据库? 如何在不同的活动中使用它?

[英]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. 为此,我需要使用数据库,我需要为餐厅建议的产品构建表,为不同的CategoryUsers表和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: 简短摘要:

  • make your class a subclass of SQLiteOpenHelper 使您的类成为SQLiteOpenHelper的子类
  • override onCreate(SQLiteDatabase db) 覆盖onCreate(SQLiteDatabase db)
  • perform your 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.

相关问题 如何通过不同的活动访问相同的数据库? - How do I access the same database from different activities? 如何在 android studio 中使用 recyclerview 显示我的数据库? - How do i display my database using recyclerview in android studio? Android:如何创建数据库管理类的对象然后使用它? - Android: How do I create an object of a database admin class and then use it? 如何在 Android 中创建/打开数据库? - How do I create/open a database in Android? 我如何创建这个数据库? - How do I create this database? 如何在 Android Studio 中跨活动保存变量值 - How do I save variable values across Activities in Android Studio 如何在Android Studio的问题数据库中添加图像? - How do I add images in my questions database in Android Studio? 如何访问 firebase 数据库以在 android 工作室中进行登录活动 - how do I access firebase database for login activity in android studio 在android中,如何在每个标签内创建不同的片段/活动堆栈,这些片段/活动堆栈会在所有标签更改之间持续存在? - In android how do I create different fragments/activities stacks within each tab that persist across tab changes? 如何为Android活动的某些部分创建公共代码? - How do I create common code for parts of Android activities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM