在databasehelper类中插入edittext
这是我的数据库类。在这里,我想在运行时将数据插入到数据库中。另一个类我在单击按钮时有 edittext,edittext 值已插入数据库但未插入,如何在此类中完成?
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "employee_directory1";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
/*
public void onCreate(SQLiteDatabase db) {
/*
* Create the employee table and populate it with sample data.
* In step 6, we will move these hardcoded statements to an XML document.
*/
String sql = "CREATE TABLE IF NOT EXISTS employee (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"firstName TEXT, " +
"lastName TEXT, " +
"title TEXT, " +
"officePhone TEXT, " +
"cellPhone TEXT, " +
"email TEXT, " +
"managerId INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("firstName", "John");
values.put("lastName", "Smith");
values.put("title", "CEO");
values.put("officePhone", "617-219-2001");
values.put("cellPhone", "617-456-7890");
values.put("email", "[email protected]");
db.insert("employee", null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS employees");
onCreate(db);
}
}
This is My Database Class. In this I want to insert the data in to the database in the run time. Another class I have edittext while clicking the button the edittext value is inserted in to the database but it is not inserted, how it can be done in this class?
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "employee_directory1";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
/*
public void onCreate(SQLiteDatabase db) {
/*
* Create the employee table and populate it with sample data.
* In step 6, we will move these hardcoded statements to an XML document.
*/
String sql = "CREATE TABLE IF NOT EXISTS employee (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"firstName TEXT, " +
"lastName TEXT, " +
"title TEXT, " +
"officePhone TEXT, " +
"cellPhone TEXT, " +
"email TEXT, " +
"managerId INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("firstName", "John");
values.put("lastName", "Smith");
values.put("title", "CEO");
values.put("officePhone", "617-219-2001");
values.put("cellPhone", "617-456-7890");
values.put("email", "[email protected]");
db.insert("employee", null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS employees");
onCreate(db);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参考此代码并更正您的更改
apater.java
refer this code and correct your changes
apater.java