gpt4 book ai didi

java - SQLiteOpenHelper : OnCreate Function not called

转载 作者:行者123 更新时间:2023-11-29 20:03:23 26 4
gpt4 key购买 nike

<分区>

即使在构造函数中调用 getWritableDatabase() 之后,OnCreate() 也没有被调用

打包 com.example.user.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB_Test dbTest = new DB_Test(getApplicationContext());
}
}

尝试在构造函数中调用 getWritableDatabase(),但不起作用

package com.example.user.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
* Created by user on 11-Mar-16.
*/
public class DB_Test extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public DB_Test(Context context) {
super(context, DATABASE_NAME, null,DATABASE_VERSION);
Log.v("MainActivity", "Constructor Working");
getWritableDatabase();

}

@Override
public void onCreate(SQLiteDatabase db) {
Log.v("MainActivity","onCreate Working");
String CREATE_PRODUCTS_TABLE = "CREATE TABLE products(column_id INTEGER PRIMARY KEY,column_product_name TEXT,column_quantity INTEGER)";
db.execSQL(CREATE_PRODUCTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v("MainActivity", "OnUpgrade Working");
db.execSQL("DROP TABLE IF EXISTS products");
onCreate(db);
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com