在android中向数据库添加值

时间:2022-03-13 08:04:23

I am doing a project which is based on storing the employee details, like name, id, log in time and log out time. So I created a database to store the details of the employee, my problem is that i am getting the values dynamically from the user in different screens, i have problem with storing the values into the database. now am passing all the values into one screen and storing them in that class, even though i am not getting the log out time of the employee. Now am using the below code to add the values into database.

我正在做一个基于存储员工详细信息的项目,比如姓名、id、登录时间和退出时间。所以我创建了一个数据库来存储员工的详细信息,我的问题是我在不同的屏幕上从用户那里动态地获取值,我在将值存储到数据库中有问题。现在,我将所有的值都传递到一个屏幕,并将它们存储在那个类中,即使我没有得到雇员的退出时间。现在使用下面的代码将值添加到数据库中。

db.addDetails(new Contact(client_id, empn, timein, "6.30PM"));

Is there any way to store the values into database screen by screen for the same user? Thanks in advance.

是否有方法将这些值存储到数据库屏幕中,以供同一用户使用?提前谢谢。

2 个解决方案

#1


0  

try like this..

这样的尝试。

public long createemployee(String name) {
    ContentValues values = new ContentValues(1);
    values.put(sqlitehelper.COLUMN_NAME,name);
    long insertId = database.insert(sqlitehelper.LIBRARY, null,values);
     return insertId;
} 

now put this in your first screen.. you will get the id of the entry in database.. now pass this id to next activities using intents.. then in next activity store time in value like this into database..and all other values can be updated in similar way in respective activities.. on passing the id from first activity

现在把这个放到你的第一个屏幕上。你会在数据库中找到条目的id。现在使用意图将这个id传递给下一个活动。然后在下一个活动中将时间值存储到数据库中。所有其他值都可以在相应的活动中以类似的方式更新。从第一个活动传递id时

String strFilter = "_id=" + Id;<--- id from first activity
ContentValues args = new ContentValues();
args.put(TIME_IN,TIME);
myDB.update(sqlitehelper.LIBRARY, args, strFilter, null);

#2


0  

This is the general idea:

这是总的想法:

The user knows which dataset he want to edit/change/update. So the identifier of a dataset in various ways is known to you. Possible ways:

用户知道要编辑/更改/更新哪个数据集。数据集的标识符以各种方式被你所知道。可能的方法:

  • Select the item befor you start your activity and parse it by .putExtra() to the intent
  • 选择要启动活动的项,并将其解析为.putExtra()。
  • Let the user select which dataset he wants to edit on ativity start
  • 让用户选择要在动手操作开始时编辑的数据集
  • Let the user select, where to save the information he just created on save.
  • 让用户选择,在哪里保存他在save上创建的信息。

In short: The ID is always known to the Programm.

简而言之:程序总是知道ID。

And therefore I would do something like

因此我会做一些类似的事情

if (db.hasDataset(id)) {
    db.updateDataset(id, updatedInformation);    
} else
    db.createDataset(id, newDatasetInitInformation);
}

in db.updateDataset you use .update(...) whith a where clause like

在db。updateDataset使用。update(…)在where子句中。

table.id=selectedId

table.id = selectedId

The .update(...) function only update the values in your contentValues object. If values are missing, the remain unchanged.

update(…)函数只更新contentValues对象中的值。如果值缺失,则保持不变。

in db.createDataset you just use .insert() in the way you supposently already do.

在db。createDataset,您只需使用.insert(),假定您已经这样做了。

for further Information see here

有关更多信息,请参见这里

#1


0  

try like this..

这样的尝试。

public long createemployee(String name) {
    ContentValues values = new ContentValues(1);
    values.put(sqlitehelper.COLUMN_NAME,name);
    long insertId = database.insert(sqlitehelper.LIBRARY, null,values);
     return insertId;
} 

now put this in your first screen.. you will get the id of the entry in database.. now pass this id to next activities using intents.. then in next activity store time in value like this into database..and all other values can be updated in similar way in respective activities.. on passing the id from first activity

现在把这个放到你的第一个屏幕上。你会在数据库中找到条目的id。现在使用意图将这个id传递给下一个活动。然后在下一个活动中将时间值存储到数据库中。所有其他值都可以在相应的活动中以类似的方式更新。从第一个活动传递id时

String strFilter = "_id=" + Id;<--- id from first activity
ContentValues args = new ContentValues();
args.put(TIME_IN,TIME);
myDB.update(sqlitehelper.LIBRARY, args, strFilter, null);

#2


0  

This is the general idea:

这是总的想法:

The user knows which dataset he want to edit/change/update. So the identifier of a dataset in various ways is known to you. Possible ways:

用户知道要编辑/更改/更新哪个数据集。数据集的标识符以各种方式被你所知道。可能的方法:

  • Select the item befor you start your activity and parse it by .putExtra() to the intent
  • 选择要启动活动的项,并将其解析为.putExtra()。
  • Let the user select which dataset he wants to edit on ativity start
  • 让用户选择要在动手操作开始时编辑的数据集
  • Let the user select, where to save the information he just created on save.
  • 让用户选择,在哪里保存他在save上创建的信息。

In short: The ID is always known to the Programm.

简而言之:程序总是知道ID。

And therefore I would do something like

因此我会做一些类似的事情

if (db.hasDataset(id)) {
    db.updateDataset(id, updatedInformation);    
} else
    db.createDataset(id, newDatasetInitInformation);
}

in db.updateDataset you use .update(...) whith a where clause like

在db。updateDataset使用。update(…)在where子句中。

table.id=selectedId

table.id = selectedId

The .update(...) function only update the values in your contentValues object. If values are missing, the remain unchanged.

update(…)函数只更新contentValues对象中的值。如果值缺失,则保持不变。

in db.createDataset you just use .insert() in the way you supposently already do.

在db。createDataset,您只需使用.insert(),假定您已经这样做了。

for further Information see here

有关更多信息,请参见这里