gpt4 book ai didi

android - 如何使用 EditText 将数据插入数据库?

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

我正在尝试使用 get EditText 将数据插入 SQLite 数据库,但我似乎找不到解决方案。到目前为止,我似乎无法理解 get 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);
}

提前致谢。

最佳答案

嗯,您需要实际使用您知道的 insertTitle 方法...它不会只是坐在那里做任何事情。看起来您之前尝试过,但是在错误的位置,在您从 EditText 字段设置字符串值之前。

在您获得最后一个 EditText 值之后和您的 db.close() 之前,插入以下内容:

db.insertTitle(IsbnValue, TitleValue, PublisherValue);

关于android - 如何使用 EditText 将数据插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882697/

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