使用插入功能删除数据库中的数据
Delete data in database with the insertion function
我制作了一个多行显示值的TextView,每行包含3个字段,如下图所示:
https://i.stack.imgur.com/fOZaC.png
我只想在数据库中显示整数,所以我部署的想法是我们将在每次插入时删除字符串“和 app:”,以仅保留下面代码中出现的数字:
public void AddContact(Contact c)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next();
cv.put("nom ",line);
if ((line.trim().length()>0)&& (line.trim().equals("<info>")) &&(line.trim().equals("app:"))){
db.delete("contact",null,null);
db.insert("contact", null, cv);
}
}
但它不起作用。
有什么办法吗?
您可以使用 substring()
从每个 line
中删除前 10 个字符
public void AddContact(Contact c) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next().trim();
if (line.startsWith("<info>app:")) {
line = line.substring(10).trim();
}
if (line.length() > 0) {
cv.put("nom", line);
db.insert("contact", null, cv);
}
}
}
另一种方法是删除所有非数字字符:
public void AddContact(Contact c) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next().replaceAll("[^\d]", "");
if (line.length() > 0) {
cv.put("nom", line);
db.insert("contact", null, cv);
}
}
}
我制作了一个多行显示值的TextView,每行包含3个字段,如下图所示: https://i.stack.imgur.com/fOZaC.png 我只想在数据库中显示整数,所以我部署的想法是我们将在每次插入时删除字符串“和 app:”,以仅保留下面代码中出现的数字:
public void AddContact(Contact c)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next();
cv.put("nom ",line);
if ((line.trim().length()>0)&& (line.trim().equals("<info>")) &&(line.trim().equals("app:"))){
db.delete("contact",null,null);
db.insert("contact", null, cv);
}
}
但它不起作用。 有什么办法吗?
您可以使用 substring()
从每个 line
public void AddContact(Contact c) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next().trim();
if (line.startsWith("<info>app:")) {
line = line.substring(10).trim();
}
if (line.length() > 0) {
cv.put("nom", line);
db.insert("contact", null, cv);
}
}
}
另一种方法是删除所有非数字字符:
public void AddContact(Contact c) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Scanner s = new Scanner(c.getNom());
while (s.hasNextLine()){
String line = s.next().replaceAll("[^\d]", "");
if (line.length() > 0) {
cv.put("nom", line);
db.insert("contact", null, cv);
}
}
}