gpt4 book ai didi

java - 将逗号分隔的列表粘贴到 EditText 中并存储在 SQLite TABLE 的多行中

转载 作者:行者123 更新时间:2023-11-29 23:17:20 26 4
gpt4 key购买 nike

我想知道如何在 EditText 字段中粘贴一个逗号分隔的列表 (test1,test2,test3,test4),然后单击一个按钮将其存储在我的 SQLite 数据库表中,每个列表都在它自己的行中。这将是理想的,这样拥有大列表 (50-100) 的人就可以批量插入数据。现在我必须要它在我的表中插入一个名字。

数据库助手.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");
}
}

最佳答案

以下是使用字符串的 splitaddData 的非常基本的替换。 将字符串分解为逗号分隔值并插入它们的方法:-

/**
* 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。

  • 以上代码为原理代码,尚未经过测试或运行,因此可能包含一些错误。

关于java - 将逗号分隔的列表粘贴到 EditText 中并存储在 SQLite TABLE 的多行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55094144/

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