我正在尝试使用get EditText将数据插入到SQLite数据库中,但我似乎找不到解决方案。到目前为止,我似乎无法理解EditText似乎如何工作。我尝试过使用其中一种解决方案,但我无法理解如何使用它。Inserting EditText & Spinner data into SQLite。
这是我的代码。
DatabaseActivity.java
public class DatabaseActivity extends Activity {
DBAdapter db = new DBAdapter(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submitbutton = (Button) findViewById(R.id.submitbutton);
submitbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
db.open();
//long id;
//id = db.insertTitle("isbn", "title", "publisher");
EditText editTextIsbn = (EditText)findViewById(R.id.EditTextIsbn);
String IsbnValue = editTextIsbn.getText().toString();
EditText editTextTitle = (EditText)findViewById(R.id.EditTextTitle);
String TitleValue = editTextTitle.getText().toString();
EditText editTextPublisher = (EditText)findViewById(R.id.EditTextPublisher);
String PublisherValue = editTextPublisher.getText().toString();
db.close();
}
});
}
}
DBAdapter.java
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_ISBN = "isbn";
public static final String KEY_TITLE = "title";
public static final String KEY_PUBLISHER = "publisher";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "books";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "isbn text not null, title text not null, "
+ "publisher text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
//---insert a title into the database---
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}
提前致谢。
答案 0 :(得分:3)
获取最后一个EditText值之后和db.close()之前,插入以下内容:
db.insertTitle(IsbnValue, TitleValue, PublisherValue);