我想知道如何在EditText字段中粘贴逗号分隔的列表(test1,test2,test3,test4),然后单击按钮将其存储在我的SQLite数据库表中,并且每个表都位于其自己的行上。这是理想的选择,这样列表很大的人(50-100)可以批量插入数据。现在,我要在表中插入一个名称。
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "hashtag_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 +" TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
/**
* Add data to the table
* @param item
* @return
*/
public boolean addData(String item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Gets the data from the table
* @return
*/
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Gets the ID from the table
* @param name
* @return
*/
public Cursor getItemID(String name) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query,null);
return data;
}
/**
* Updates the name from the table
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 + " = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" + " AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: setting name to " + newName);
db.execSQL(query);
}
/**
* Deletes the name from the table
* @param id
* @param name
*/
public void deleteName(int id, String name) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE " + COL1 + " = '" + id + "'" + " AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
db.execSQL("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE NAME = '"+TABLE_NAME+"'");
}
}
ListView.java(现在editText和按钮在其中)
//Adds new hashtag to list and prompts if nothing is entered
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newEntry = editText.getText().toString();
if (editText.length() != 0) {
addData(newEntry);
editText.setText("");
} else {
toastMessage("you must put something in the text field");
}
}
});
populateListView();
}
/**
* Adds new data into the Database
* @param newEntry
*/
public void addData(String newEntry) {
boolean insertData = mDatabaseHelper.addData(newEntry);
if (insertData) {
toastMessage("Successfully inserted");
recreate();
} else {
toastMessage("Whoops, something went wrong");
}
}
答案 0 :(得分:1)
以下是 addData 的非常基本的替代,它使用字符串的 split 方法将字符串分成逗号分隔的值并将其插入:-
/**
* Add data to the table
*
* @param item
* @return
*/
public boolean addData(String item) {
String[] splitdata = item.split(","); //<<<<<<<<<< split the input string
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
boolean result = true;
db.beginTransaction(); //<<<<<<<<<< prepare to do in a single transaction
// Loop through each string inserting an entry into the database
for (String s : splitdata) {
contentValues.clear(); //<<<<<<<<<< clear any existing values to be safe
contentValues.put(COL2, s);
if (db.insert(TABLE_NAME, null, contentValues) < 1) {
result = false;
}
}
if (result) {
db.setTransactionSuccessful(); //<<<<<<<<<< only set the transaction successful if all inserts worked
}
db.endTransaction();
return result;
}
请注意,如果有任何失败的插入,这将回滚所有插入,并另外返回false。