这是 MainActivity.java
package com.example.sqltwo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MainActivity extends Main {
public static final String KEY_NAMES = "";
public static final String KEY_VALUES = "";
private static final String DATABASE_NAME = "Database";
private static final String DATABASE_TABLE = "DBTable";
private static final int DATABASE_VERSION = 1;
private DbHelper qhelper;
private final Context qcontext;
private SQLiteDatabase qdatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_NAMES + " TEXT NOT NULL, " +
KEY_VALUES + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public MainActivity(Context c){
qcontext = c;
}
public MainActivity open() throws SQLException{
qhelper = new DbHelper(qcontext);
qdatabase = qhelper.getWritableDatabase();
return this;
}
public void close(){
qhelper.close();
}
public long createEntry(String name, String value) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAMES, name);
cv.put(KEY_VALUES, value);
return qdatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[]{KEY_NAMES, KEY_VALUES};
Cursor c = qdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iValue = c.getColumnIndex(KEY_VALUES);
int iName = c.getColumnIndex(KEY_NAMES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iName) + "\n" + c.getString(iValue) + "\n";
}
return result;
}
}
这是 Main.java
package com.example.sqltwo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity implements OnClickListener{
Button sqlUpdate, sqlView, sqlQuery;
EditText sqlName, sqlValue, sqlQuery_value;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlUpdate = (Button) findViewById(R.id.buttonupdate);
sqlName = (EditText) findViewById(R.id.editname);
sqlValue = (EditText) findViewById(R.id.editvalue);
sqlView = (Button) findViewById(R.id.buttonview);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
sqlQuery = (Button) findViewById(R.id.buttonsearch);
sqlQuery_value = (EditText) findViewById(R.id.editquery);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.buttonupdate:
String name = sqlName.getText().toString();
String value = sqlValue.getText().toString();
MainActivity entry = new MainActivity(Main.this);
entry.open();
entry.createEntry(name, value);
entry.close();
break;
case R.id.buttonview:
Intent intentone = new Intent();
intentone.setClassName("com.example.sqltwo", "com.example.sqltwo.View");
startActivity(intentone);
break;
}
}
}
我的错误
02-28 09:23:07.334: E/AndroidRuntime(827): android.database.sqlite.SQLiteException: duplicate column name: TEXT (code 1): , while compiling: CREATE TABLE DBTable ( TEXT NOT NULL, TEXT NOT NULL);
我要这个做什么?
您可以输入名称和值并将数据更新到数据库中。视图功能尚未编码。我需要输入数据,没有崩溃。我需要在 MainActivity 中设置一个查询系统,它将与 Main 中的 switch-case 系统一起使用。如何做到这一点,当我输入查询时,它会将数据发送到 MainActivity,运行搜索,然后返回有效值列表?
-Extra:如何使每个查询返回的数据集显示在一个表行中,并在出现更多数据时添加行?
-Extra:我可以请人实时帮助我吗?要问的问题太多了?