本文介绍了动态创建表单(On Button Click)在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在写一个应用程序,它需要动态创建表。我写了以下代码:
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:
解决方案
在某些地方你使用 db
在某些地方 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();
}
I am writing an application and it needs tables to be created dynamically.I've written the following code :
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();
}
But it crashes the application. Cannot find whats wrong with it.
Logcat:
解决方案
At some places you were using db
at some places dbs
Try this code
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();
}
这篇关于动态创建表单(On Button Click)在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!