提问者:小点点

将数据存储在数据库Android中


我创建了一个TextView,它以多行显示值
,我希望将该值保留在SQLite数据库中。这是我使用的代码:

public void AddContact(Contact c)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put("nom",c.getNom());
    Scanner s = new Scanner(String.valueOf(cv));
    while (s.hasNextLine()){
        String line = s.nextLine();
        db.insert("contact",line,cv);
        
    }

问题是当我保存值时,整个值被插入到一个单元格中。我希望每行的值分开,然后插入到每行的单个单元格中,尽管我使用扫描器方法插入数据,但它不起作用。那有什么办法吗?


共1个答案

匿名用户

在您的代码中,这一行:

cv.put("nom",c.getNom());

c.getnom()的完整字符串分配给ContentValues对象,这是您在表中插入的内容。
您必须将c.getnom()的每一行分配给CV并将其插入,每次在while循环的迭代中插入一行:

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.nextLine();
        cv.put("nom", line); 
        db.insert("contact", null, cv);
    }      
}