gpt4 book ai didi

java - 我创建了一个空数据库,里面没有任何表和数据

转载 作者:行者123 更新时间:2023-12-02 09:54:25 24 4
gpt4 key购买 nike

我的数据库有问题。没有任何表和数据,只有空数据库。我应该在代码中更改什么?我想创建一个包含硬编码值的数据库,因为我想稍后在微调器中使用它们。

Database class

public class Database extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "CurrencyExchange.db";
private static final String CURRENCY_TABLE = "currency_table";



public Database(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+CURRENCY_TABLE+ " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, BGN TEXT, NZD TEXT, ILS TEXT, RUB TEXT, CAD TEXT, USD TEXT, PHP TEXT, CHF TEXT, ZAR TEXT, AUD TEXT, JPY TEXT, TRY TEXT, HKD TEXT, MYR TEXT, THB TEXT, HRK TEXT, NOK TEXT, IDR TEXT, DKK TEXT, CZK TEXT, HUF TEXT, GBP TEXT, MXN TEXT, KRW TEXT, ISK TEXT, SGD TEXT, BRL TEXT, PLN TEXT, INR TEXT, RON TEXT, CNY TEXT, SEK TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+CURRENCY_TABLE);
onCreate(db);
}

public boolean insertData(){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

final String insert_data = "INSERT INTO "+CURRENCY_TABLE+ " VALUES (0,'BGN','NZD','ILS','RUB','CAD','USD','PHP','CHF','ZAR','AUD','JPY','TRY','HKD','MYR','THB','HRK','NOK','IDR','DKK','CZK','HUF','GBP','MXN','KRW','ISK','SGD','BRL','PLN','INR','RON','CNY','SEK')";
contentValues.put(CURRENCY_TABLE, insert_data);
return database.insert(CURRENCY_TABLE, null, contentValues) != -1;
}

主要 Activity

    public class MainActivity extends AppCompatActivity {

Database database;

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

database = new Database(this);
database.insertData();

最佳答案

您想要做的(请参阅下文了解为什么这可能不是您想要做的)是将 insertData 方法更改为:-

public boolean insertData( String bgn, String nzd, String ils, String run, String cad,
String usd, String php, String chf, String zar, String aud, String jpy,
String TRY, String hkd, String myr, String thb, String hrk, String nok,
String idr, String dkk, String czk, String huf, String gbp, String mxn,
String krw, String isk, String sgd, String brl, String pln, String inr,
String ron, String cny, String sek){

SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("BGN",bgn);
contentValues.put("NZD",nzd);
contentValues.put("ILS",ils);
contentValues.put("RUB",run);
contentValues.put("CAD",cad);
contentValues.put("USD",usd);
contentValues.put("PHP",php);
contentValues.put("CHF",chf);
contentValues.put("ZAR",zar);
contentValues.put("AUD",aud);
contentValues.put("JPY",jpy);
contentValues.put("TRY",TRY);
contentValues.put("HKD",hkd);
contentValues.put("MYR",myr);
contentValues.put("BGN",bgn);
contentValues.put("THB",thb);
contentValues.put("HRK",hrk);
contentValues.put("NOK",nok);
contentValues.put("IDR",idr);
contentValues.put("DKK",dkk);
contentValues.put("CZK",czk);
contentValues.put("HUF",huf);
contentValues.put("GBP",gbp);
contentValues.put("MXN",mxn);
contentValues.put("KRW",krw);
contentValues.put("ISK",isk);
contentValues.put("SGD",sgd);
contentValues.put("BRL",brl);
contentValues.put("PLN",pln);
contentValues.put("INR",inr);
contentValues.put("RON",ron);
contentValues.put("CNY",cny);
contentValues.put("SEK",sek);
return database.insert(CURRENCY_TABLE, null, contentValues) != -1;
}
  • 当然,这假设您希望使用 VALUES 为每列插入一个值,而括号内没有逗号分隔的列列表指定所有列
  • 请注意,没有为第一个列 ID 提供任何值,然后将按照 ID 列的典型用途自动生成该值。

添加此方法

public Cursor getAllRowsFromCurrencyTable() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(CURRENCY_TABLE,null,null,null,null,null,null);
}

并在 Activity 中使用以下内容并运行一次:-

public class MainActivity extends AppCompatActivity {

Database database;

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

database = new Database(this);
// Add a row for tesing
database.insertData("A","B","C","D",
"E","F","G","H","I",
"J","K","L","M","N",
"O","P","Q","R","S",
"T","U","V","W","X",
"Y","Z","AA","AB","AC",
"AD","AE","AF"
);
// Get all the rows (1) and dump the resultant Cursor
Cursor csr = database.getAllRowsFromCurrencyTable();
DatabaseUtils.dumpCursor(csr);
csr.close(); // Should always close a Cursor when done with it.

}
}

结果:-

2019-05-13 19:37:28.974 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@76cd9ae
2019-05-13 19:37:28.974 I/System.out: 0 {
2019-05-13 19:37:28.975 I/System.out: ID=1
2019-05-13 19:37:28.975 I/System.out: BGN=A
2019-05-13 19:37:28.975 I/System.out: NZD=B
2019-05-13 19:37:28.975 I/System.out: ILS=C
2019-05-13 19:37:28.975 I/System.out: RUB=D
2019-05-13 19:37:28.975 I/System.out: CAD=E
2019-05-13 19:37:28.975 I/System.out: USD=F
2019-05-13 19:37:28.975 I/System.out: PHP=G
2019-05-13 19:37:28.975 I/System.out: CHF=H
2019-05-13 19:37:28.975 I/System.out: ZAR=I
2019-05-13 19:37:28.975 I/System.out: AUD=J
2019-05-13 19:37:28.975 I/System.out: JPY=K
2019-05-13 19:37:28.976 I/System.out: TRY=L
2019-05-13 19:37:28.976 I/System.out: HKD=M
2019-05-13 19:37:28.976 I/System.out: MYR=N
2019-05-13 19:37:28.976 I/System.out: THB=O
2019-05-13 19:37:28.976 I/System.out: HRK=P
2019-05-13 19:37:28.976 I/System.out: NOK=Q
2019-05-13 19:37:28.976 I/System.out: IDR=R
2019-05-13 19:37:28.976 I/System.out: DKK=S
2019-05-13 19:37:28.976 I/System.out: CZK=T
2019-05-13 19:37:28.976 I/System.out: HUF=U
2019-05-13 19:37:28.976 I/System.out: GBP=V
2019-05-13 19:37:28.977 I/System.out: MXN=W
2019-05-13 19:37:28.977 I/System.out: KRW=X
2019-05-13 19:37:28.977 I/System.out: ISK=Y
2019-05-13 19:37:28.977 I/System.out: SGD=Z
2019-05-13 19:37:28.977 I/System.out: BRL=AA
2019-05-13 19:37:28.977 I/System.out: PLN=AB
2019-05-13 19:37:28.977 I/System.out: INR=AC
2019-05-13 19:37:28.977 I/System.out: RON=AD
2019-05-13 19:37:28.977 I/System.out: CNY=AE
2019-05-13 19:37:28.977 I/System.out: SEK=AF
2019-05-13 19:37:28.977 I/System.out: }
2019-05-13 19:37:28.977 I/System.out: <<<<<
  • 注意 ID= 1,自动生成的 ID 接下来可能是 2,然后是 3,依此类推(不能保证数字会按顺序排列,但很有可能)。
<小时/>

但是

如你所说

I want to create an database, with hardcoded values, because i'd want to use them later in spinner.

那么您可能不想使用货币代码列,而是希望每种货币有 x 行 1。

因此,也许可以考虑这个工作示例,其中一个 Spinner 列出了货币代码,并在您选择不同的代码时发出一个 Toast :-

修改后的Database.java

public class Database extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "CurrencyExchange.db";
public static final String CURRENCY_TABLE = "currency_table";
public static final String CURRENCY_ID_COLUMN = BaseColumns._ID; // Allows Spinner/ListView with Cursor Adapter
public static final String CURRENCY_CODE_COLUMN = "currency_code";

// The Currency Codes
public static final String[] currency_codes = new String[]{
"BGN", "NZD", "ILS", "RUB", "CAD", "USD", "PHP", "CHF",
"ZAR", "AUD", "JPY", "TRY", "HKD", "MYR", "THB", "HRK",
"NOK", "IDR", "DKK", "CZK", "HUF", "GBP", "MXN", "KRW",
"ISK ", "SGD", "BRL", "PLN", "INR", "RON", "CNY", "SEK"
};

public Database(Context context) {
super(context, DATABASE_NAME, null, 2); // Version changed so will drop old table and recreate new
}

@Override
public void onCreate(SQLiteDatabase db) {
/* OLD
db.execSQL("CREATE TABLE " + CURRENCY_TABLE + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"BGN TEXT, NZD TEXT, ILS TEXT, RUB TEXT, CAD TEXT, USD TEXT, PHP TEXT, CHF TEXT, " +
"ZAR TEXT, AUD TEXT, JPY TEXT, TRY TEXT, HKD TEXT, MYR TEXT, THB TEXT, HRK TEXT, " +
"NOK TEXT, IDR TEXT, DKK TEXT, CZK TEXT, HUF TEXT, GBP TEXT, MXN TEXT, KRW TEXT, " +
"ISK TEXT, SGD TEXT, BRL TEXT, PLN TEXT, INR TEXT, RON TEXT, CNY TEXT, SEK TEXT)");
*/
db.execSQL("CREATE TABLE " + CURRENCY_TABLE + "(" +
CURRENCY_ID_COLUMN + " INTEGER PRIMARY KEY, " + // NO NEED FOR AUTOINCREMENT (it's inefficient)
CURRENCY_CODE_COLUMN + " TEXT UNIQUE " + // UNIQUE means will not add duplicates
")");
addAllCurrencyCodes(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CURRENCY_TABLE);
onCreate(db);
}

private void addAllCurrencyCodes(SQLiteDatabase database) {
if (database == null) {
database = this.getWritableDatabase();
}
database.beginTransaction();
for (String currency_code: currency_codes) {
addCurrencyCode(currency_code,database);
}
database.setTransactionSuccessful();
database.endTransaction();
}

public void addCurrencyCode(String code, SQLiteDatabase database) {

if (database == null) {
database = this.getWritableDatabase();
}
ContentValues contentValues = new ContentValues();
contentValues.put(CURRENCY_CODE_COLUMN,code);
database.insert(CURRENCY_TABLE,null,contentValues);
}

public Cursor getAllRowsFromCurrencyTable() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(CURRENCY_TABLE,null,null,null,null,null,null);
}
}

Activity 的布局(即添加了 Spinner)activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<Spinner
android:id="@+id/currrency_code_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>

</LinearLayout>

最后是更改后的 Activity MainActivity.java

public class MainActivity extends AppCompatActivity {

Database database;
Spinner currency_code_selector;
SimpleCursorAdapter mSCA;
Cursor csr;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currency_code_selector = this.findViewById(R.id.currrency_code_selector);

database = new Database(this);
// For testing only
csr = database.getAllRowsFromCurrencyTable();
DatabaseUtils.dumpCursor(csr);
manageCurrencyCodeSpinner();
}

private void manageCurrencyCodeSpinner() {
csr = database.getAllRowsFromCurrencyTable();
if (mSCA == null) {
mSCA = new SimpleCursorAdapter(
this, // The context
android.R.layout.simple_dropdown_item_1line, // The layout (stock layout used)
csr, // The Cursor for the Spinner
new String[]{Database.CURRENCY_CODE_COLUMN}, // The columns holding the data
new int[]{android.R.id.text1}, // The View into which the data is placed
0 // 0 is fine
);
currency_code_selector.setAdapter(mSCA); // Tie the adapter to the Spinner
// Setup selection handling
currency_code_selector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (view == null) return;
Toast.makeText(view.getContext(),
"You have just selected the currency code " +
csr.getString(csr.getColumnIndex(Database.CURRENCY_CODE_COLUMN)) +
". The ID is " + String.valueOf(id),
Toast.LENGTH_SHORT ).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
} else {
mSCA.swapCursor(csr); // if spinner already setup refresh the contents
}
}

@Override
protected void onDestroy() {
super.onDestroy();
csr.close(); //<<<<<<<<<< close the cursor when done with it
}

@Override
protected void onResume() {
super.onResume();
manageCurrencyCodeSpinner(); // refresh the Spinner as data may have changed
}
}

结果:-

enter image description here

  • 显然您会增强外观,这只是一个演示
  • 您只需相信我的话,Toast 就可以工作(可以捕获它)

  • 注意要部署此更改,您应该执行以下操作之一:-

    • 卸载应用
    • 删除应用数据
    • 增加版本号,即将 super(context, DATABASE_NAME, null, 1); 更改为 super(context, DATABASE_NAME, null, 2);

关于java - 我创建了一个空数据库,里面没有任何表和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56108132/

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