我正在尝试创建一个可以存储数据的应用程序,但数据架构由用户提供。所以我需要将表名和列的名称传递给DatabaseHelper.java,以便在onCreate方法中使用它们。
我发现你可以使用Intent传递字符串,但是这里没有用,因为事实证明它只适用于活动之间。
所以我可以从用户那里获取信息,但是在用逗号分隔名称的字符串中。但是我无法将字符串发送到DatabaseHelper.java以在onCreate中执行。
有办法做到这一点吗?或者我是否必须使用其他方法?或者这是不可能的事情?
先谢谢,所有人 最好
获取信息并处理信息的方法:
/* Called when the user clicks the Finalize button */
public void createSchema(View view) {
String create_table = ""; // Store table name and columns name
schemaName = (EditText)findViewById(R.id.create_schema_name);
String schemaNameString = schemaName.getText().toString().trim();
fieldsContainer = (LinearLayout)findViewById(R.id.fieldsContainer);
int childCount = fieldsContainer.getChildCount();
if (schemaNameString.length() != 0) {
// When the name is entered
if (childCount > 0) {
// When there is at least one field entered
create_table += this.slug(schemaNameString) + ",_id,timestamp,latitude,longitude,";
for (int i = 0; i < childCount; i++) {
View childView = fieldsContainer.getChildAt(i);
TextView childTextView = (TextView)(childView.findViewById(R.id.added_schema_field));
String childTextViewString = childTextView.getText().toString();
create_table += this.slug(childTextViewString) + ",";
}
create_table = create_table.substring(0, create_table.length() - 1);
Toast.makeText(getApplicationContext(), create_table, Toast.LENGTH_SHORT).show();
// Doesn't work
// Intent intent = new Intent(this, DatabaseHelper.java);
// intent.putExtra(CREATE_TABLE, create_table);
// startActivity(intent);
} else {
// No field entered
Toast.makeText(getApplicationContext(), "There must be at least one field entered to " + schemaNameString + ".", Toast.LENGTH_SHORT).show();
}
} else {
// No name entered
Toast.makeText(getApplicationContext(), "No name entered for the schema", Toast.LENGTH_SHORT).show();
}
}
我的DatabaseHelper.java:
package com.example.draft;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "myDB";
// Doesn't work
// Intent intent = getIntent();
// String create_table = intent.getStringExtra(CreateSchemaActivity.CREATE_TABLE);
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("");
onCreate(db);
}
}
答案 0 :(得分:2)
您无法使用SQLiteOpenHelper
。您需要自己管理数据库及其升级,直接使用SQLiteDatabase
。