gpt4 book ai didi

android - 在 Android 中动态创建表格(点击按钮)

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:26 26 4
gpt4 key购买 nike

我正在编写一个应用程序,它需要动态创建表。我写了以下代码:

public void CreateDynamicTables(String Table_Name, String Contact_ID, String Display_Name){
dbs.execSQL("DROP TABLE IF EXISTS " + Table_Name);
String query = "CREATE TABLE " + Table_Name + "(" + CID + " TEXT PRIMARY KEY, " + DName + " TEXT);";
dbs.execSQL(query);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CID, Contact_ID);
cv.put(DName, Display_Name);
db.insert(Table_Name, null, cv);
db.close();
}

但它使应用程序崩溃。找不到问题所在。

日志:

Logcat

最佳答案

At some places you were using db at some places dbs

试试这段代码

public void CreateDynamicTables(String Table_Name, String Contact_ID, String Display_Name)       
{

dbs = this.getWritableDatabase();
dbs.execSQL("DROP TABLE IF EXISTS " + Table_Name);
String query = "CREATE TABLE " + Table_Name + "(" + CID + " TEXT PRIMARY KEY, " + DName + " TEXT);";
dbs.execSQL(query);
dbs = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CID, Contact_ID);
cv.put(DName, Display_Name);
dbs.insert(Table_Name, null, cv);
dbs.close();
}

关于android - 在 Android 中动态创建表格(点击按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068518/

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