在不同的类中创建SQLite表&在不同的类中执行CRUD操作

时间:2018-04-02 07:58:15

标签: java android sqlite

我在SQLite数据库中共有5个表如果我在 DatabaseHalper.class 中创建一个全部表并执行所有 CRUD((创建,读取,更新和删除))< / strong>在此课程中操作,这看起来像大数据

Table Is Like: Company, Contact, ToDo。等

  • 首先,我想在 DatabaseHalper.class
  • 中创建一个多表
  • 第二个创建另一个类就像使用表名 ComapnyDB.class 一样 在此课程中,我想为 Company 表执行CRUD操作,同时所有查询都针对公司表执行

  • Contact 表的第3个相同。在 DatabaseHalper.class 中创建表格 并在此类中创建一个新类,如 Contact.class ,执行所有CURD操作并执行其他操作

所以我的代码分为不同的不同类

  • 示例下面的课程是 DatabseHalper.class ,在此课程中我将创建数据库表。

DatabaseHalper.class

public class DatabaseHelper extends SQLiteOpenHelper {

    // Logcat tag
    private static final String LOG = "DatabaseHelper";

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

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    //Tables Name
    private static final String COMPANY = "company";
    private static final String CONTACTS = "contacts";
    private static final String EVENT = "event";
    private static final String TODO = "todo";
    private static final String USER = "user";

    // Common column names
    private static final String KEY_ID = "id";

    //Companies Table Column Name
    private static final String KEY_COMPANY_ID = "CompanyId";
    private static final String KEY_COMPANY_REFERENCE_ID = "ReferenceId";
    private static final String KEY_COMPANY_NAME = "CompanyName";
    private static final String KEY_COMPANY_WEBSITE = "CompanyWebsite";
    private static final String KEY_COMPANY_EMAIL = "CompanyEmail";
    private static final String KEY_COMPANY_PHONE_HOME = "CompanyPhoneHome";
    private static final String KEY_COMPANY_PHONE_PRIMARY = "CompanyPhonePrimary";
    private static final String KEY_COMPANY_ADDRESS1 = "CompanyAddress";
    private static final String KEY_COMPANY_ADDRESS2 = "CompanyAddressSecondary";
    private static final String KEY_COMPANY_CITY = "CompanyCity";
    private static final String KEY_COMPANY_STATE = "CompanyState";
    private static final String KEY_COMPANY_ZIP = "CompanyZip";
    private static final String KEY_COMPANY_COUNTRY = "CompanyCountry";

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

    // Table Create Statements
    // Todo table create statement
    private static final String CREATE_TABLE_COMPANY = "CREATE TABLE "
            + COMPANY + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_COMPANY_ID + " INTEGER,"
            + KEY_COMPANY_REFERENCE_ID + " INTEGER,"
            + KEY_COMPANY_NAME + " VARCHAR,"
            + KEY_COMPANY_WEBSITE + "VARCHAR,"
            + KEY_COMPANY_EMAIL + "VARCHAR,"
            + KEY_COMPANY_PHONE_HOME + "VARCHAR,"
            + KEY_COMPANY_PHONE_PRIMARY + "VARCHAR,"
            + KEY_COMPANY_ADDRESS1 + "VARCHAR,"
            + KEY_COMPANY_ADDRESS2 + "VARCHAR,"
            + KEY_COMPANY_CITY + "VARCHAR,"
            + KEY_COMPANY_STATE + "VARCHAR,"
            + KEY_COMPANY_ZIP + "INTEGER,"
            + KEY_COMPANY_COUNTRY + "VARCHAR" + ")";


    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE_COMPANY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

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

现在我要创建 CompanyDB.class CURD 操作但如何访问 DatabaseHelper.class Company.class

提前致谢:)

1 个答案:

答案 0 :(得分:1)

以下是将表格与DatabaseHelper分开的示例: -

ContactDB.java

public class ContactDB {

    public static final String TBNAME = "contact";
    public static final String COL_ID = BaseColumns._ID;
    public static final String COL_NAME = "contact_name";
    public static final String COL_EMAIL = "contact_email";

    public static String getTableCreatSQL() {
        return "CREATE TABLE IF NOT EXISTS " + TBNAME + "(" +
                COL_ID + " INTEGER PRIMARY KEY, " +
                COL_NAME + " TEXT," +
                COL_EMAIL + " TEXT" +
                ")";
    }

    public static long addContact(SQLiteDatabase db, String contact_name, String contact_email) {
        ContentValues cv = new ContentValues();
        cv.put(COL_NAME,contact_name);
        cv.put(COL_EMAIL,contact_email);
        return db.insert(TBNAME,null,cv);
    }

    public static Cursor getAllContacts(SQLiteDatabase db) {
        return db.query(TBNAME,null,null,null,null,null,COL_NAME + " ASC," + COL_EMAIL + " ASC");
    }
}

CompanyDB.java

public class CompanyDB {

    public static final String TBNAME = "company";
    public static final String COL_ID = BaseColumns._ID;
    public static final String  COL_NAME = "company_name";


    public static String getTableCreateSQL() {
         return "CREATE TABLE IF NOT EXISTS " + TBNAME + "(" +
                 COL_ID + " INTEGER PRIMARY KEY," +
                 COL_NAME + " TEXT" +
                 ")";
    }

    public static long addCompany(SQLiteDatabase db, String company_name ) {
        ContentValues cv = new ContentValues();
        cv.put(COL_NAME,company_name);
        return db.insert(TBNAME,null,cv);
    }

    public static Cursor getAllCompanies(SQLiteDatabase db) {
        return  db.query(TBNAME,null,null,null,null,null,COL_NAME + " ASC");
    }
}

DBHelper.java(数据库助手)

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "mydatabase";
    public static final int DBVERSION = 1;

    private static SQLiteDatabase mDB;

    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CompanyDB.getTableCreateSQL());
        db.execSQL(ContactDB.getTableCreatSQL());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Cursor mAllContacts;
    Cursor mAllCompanies;

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

        SQLiteDatabase mDB = new DBHelper(this).getWritableDatabase();

        CompanyDB.addCompany(mDB,"mycompany");
        ContactDB.addContact(mDB,"Fred","Fred@email.com");
        ContactDB.addContact(mDB,"Bert","bertbloggins@bloggings.moc");

        mAllCompanies = CompanyDB.getAllCompanies(mDB);
        while (mAllCompanies.moveToNext()) {
            Log.d("COMPANY",
                    "Company Name = " +
                            mAllCompanies.getString(
                                    mAllCompanies.getColumnIndex(
                                            CompanyDB.COL_NAME
                                    )
                            )
            );
        }
        Cursor mAllContacts = ContactDB.getAllContacts(mDB);
        while (mAllContacts.moveToNext()) {
            Log.d("CONTACT",
                    "Contact Name = " +
                    mAllContacts.getString(
                            mAllContacts.getColumnIndex(
                                    ContactDB.COL_NAME
                            )
                    ) +
                    " Email = " +
                            mAllContacts.getString(
                                    mAllContacts.getColumnIndex(
                                            ContactDB.COL_EMAIL
                                    )
                            )
            );
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAllCompanies.close();
        mAllContacts.close();

    }
}
  • 这将添加公司和2个联系人(每次运行时),从数据库中检索公司和联系人,并将详细信息写入日志。

输出将是石灰(首次运行): -

04-02 09:09:42.556 1497-1497/so49607475_splittableclasses.so49607475_splittableclasses D/COMPANY: Company Name = mycompany
04-02 09:09:42.556 1497-1497/so49607475_splittableclasses.so49607475_splittableclasses D/CONTACT: Contact Name = Bert Email = bertbloggins@bloggings.moc
    Contact Name = Fred Email = Fred@email.com