- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 android 中创建 sqlit 数据库以将来自 json 的数据存储在数据库中,以便在应用程序离线时查看数据我添加代码来创建数据库和列,但我不知道如何将数据从 json 插入到我的数据库中。我应该怎么做才能将数据从 json 插入到 sqlite我应该在代码中添加什么来做到这一点?
SQLiteOpenHelper
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class BookingTabel extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "myDB";
// Books table name
private static final String TABLE_BOOKINDS = "bookings";
// Books Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_BOOKING_NUM = "booking_num";
private static final String KEY_TITLE = "title";
private static final String KEY_BOOKING_START= "booking_start";
private static final String KEY_BOOKING_END = "booking_end";
private static final String KEY_PROPERTY = "property_type";
private static final String KEY_CUSTOMER_ID = "customer_id";
private static final String KEY_DESCRIPTION = "description";
private static final String KEY_ACCEPT_STATUS = "accept_status";
private static final String KEY_BOOLING_ADDRESS = "booking_address";
private static final String KEY_PO_BOX = "po_box";
private static final String KEY_CREATED = "created";
private static final String KEY_POSTCODE = "postcode";
private static final String KEY_STATE = "state";
private static final String KEY_STREET_ADDRESS = "street_address";
private static final String KEY_STREET_NUMBER = "street_number";
private static final String KEY_SUBURB = "suburb";
private static final String KEY_UNIT_LOT_NUMBER = "unit_lot_number";
private static final String KEY_STATUS = "status";
private static final String KEY_CONVERT_STATUS = "convert_status";
private static final String KEY_QOUTE = "qoute";
private static final String KEY_SUB_TOTAL = "sub_total";
private static final String KEY_TOTAL_DISCOUNT = "total_discount";
private static final String KEY_BOOKING_DISTANCE = "booking_distance";
private static final String KEY_GST = "gst";
private static final String KEY_ORIGINAL_BOOKING_ID = "original_booking_id";
public BookingTabel(Context context) {
super(context, DB_NAME, null,DB_VERSION);
// TODO Auto-generated constructor stub
}
private static final String[] COLUMNS = {KEY_ID,KEY_BOOKING_NUM,KEY_TITLE,KEY_BOOKING_START,KEY_BOOKING_END,KEY_PROPERTY,
KEY_CUSTOMER_ID,KEY_DESCRIPTION,KEY_ACCEPT_STATUS,KEY_BOOLING_ADDRESS,KEY_PO_BOX,KEY_CREATED ,
KEY_POSTCODE,KEY_STATE,KEY_STREET_ADDRESS,KEY_STREET_NUMBER,KEY_SUBURB,KEY_UNIT_LOT_NUMBER,KEY_STATUS,
KEY_CONVERT_STATUS,KEY_QOUTE,KEY_SUB_TOTAL,KEY_TOTAL_DISCOUNT,KEY_BOOKING_DISTANCE,KEY_GST,
KEY_ORIGINAL_BOOKING_ID,};
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// SQL statement to create book table
String CREATE_BOOKINDS_TABLE =
"CREATE TABLE booking ( " + "id INTEGER PRIMARY KEY"+ "booking_num VARCHAR(250)"+ "title VARCHAR(250)"+
"booking_start DATETIME "+"booking_end DATETIME "+
"property_type VARCHAR(250) NOT NULL DEFAULT 'House'"+
"customer_id INTEGER+description TEXT"+"accept_status VARCHAR(250)"+"booking_address VARCHAR(250)" +
"po_box VARCHAR(250)"+ "created INTEGER"+ "postcode INTEGER"+"state VARCHAR(250)"+
"street_address VARCHAR(250)"+"street_number INTEGER"+"suburb VARCHAR(250)"+"unit_lot_number INTEGER"+
"status VARCHAR(250)"+"convert_status VARCHAR(250)"+"qoute FLOAT"+"sub_total FLOAT"+"total_discount FLOAT"+
"booking_distance FLOAT"+"gst FLOAT"+"original_booking_id INTEGER )";
// create books table
db.execSQL(CREATE_BOOKINDS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS bookings");
// create fresh books table
this.onCreate(db);
}
public void addBookings(Bookings booking){
Log.d("addBooking", booking.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_BOOKING_NUM,booking.getbookingnum());
values.put(KEY_TITLE,booking.gettitle());
values.put(KEY_BOOKING_START ,booking.getbookingstart() );
values.put(KEY_BOOKING_END ,booking.getbooking_end() );
values.put(KEY_PROPERTY ,booking.getpropertytype() );
values.put(KEY_CUSTOMER_ID ,booking.getcustomer_id() );
values.put( KEY_DESCRIPTION,booking.getdescription() );
values.put( KEY_ACCEPT_STATUS,booking.getaccept_status() );
values.put( KEY_BOOLING_ADDRESS,booking.getbookingaddress() );
values.put( KEY_PO_BOX,booking.getpo_box() );
values.put( KEY_CREATED,booking.getcreated() );
values.put( KEY_POSTCODE,booking.getpostcode() );
values.put( KEY_STATE,booking.getstate() );
values.put( KEY_STREET_ADDRESS,booking.getstreet_address() );
values.put( KEY_STREET_NUMBER,booking.getstreet_number() );
values.put( KEY_SUBURB,booking.getsuburb() );
values.put( KEY_UNIT_LOT_NUMBER,booking.getunit_lot_number() );
values.put( KEY_STATUS,booking.getstatus() );
values.put( KEY_CONVERT_STATUS,booking.getconvert_status() );
values.put( KEY_QOUTE,booking.getqoute() );
values.put( KEY_SUB_TOTAL,booking.getsub_total() );
values.put( KEY_TOTAL_DISCOUNT,booking.gettotal_discount() );
values.put( KEY_BOOKING_DISTANCE,booking.getbooking_distance() );
values.put( KEY_GST,booking.getgst() );
values.put( KEY_ORIGINAL_BOOKING_ID,booking.getoriginal_booking_id() );
db.insert(TABLE_BOOKINDS, null, values);
db.close();
}
}
预订
public class Bookings {
private int id;
private String booking_num;
private String title;
private String booking_start;
private String booking_end;
private String property_type;
private int customer_id;
private String description;
private String accept_status;
private String booking_address;
private String po_box;
private int created;
private int postcode;
private String state;
private String street_address;
private int street_number;
private String suburb;
private int unit_lot_number;
private String status;
private String convert_status;
private Float qoute;
private Float sub_total;
private Float total_discount;
private Float booking_distance;
private Float gst;
private int original_booking_id;
public Bookings(){}
public Bookings(String booking_num ,String title ,String booking_start ,String booking_end ,
String property_type ,int customer_id ,String description ,String accept_status ,String booking_address ,
String po_box ,int created ,int postcode ,String state ,String street_address ,int street_number ,
String suburb ,int unit_lot_number ,String status ,String convert_status ,Float qoute ,Float sub_total ,
Float total_discount ,Float booking_distance ,Float gst ,int original_booking_id){
this.booking_num = booking_num;
this.title = title;
this.booking_start = booking_start;
this.booking_end =booking_end ;
this.property_type =property_type;
this.customer_id = customer_id;
this.description = description;
this.accept_status = accept_status;
this.booking_address = booking_address;
this.po_box = po_box;
this.created = created;
this.postcode = postcode;
this.state = state;
this.street_address = street_address;
this.street_number = street_number;
this.suburb = suburb;
this.unit_lot_number = unit_lot_number;
this.status = status;
this.convert_status = convert_status;
this.qoute = qoute;
this.sub_total = sub_total;
this.total_discount = total_discount;
this.booking_distance = booking_distance;
this.gst = gst;
this.original_booking_id = original_booking_id;
}
// ---- setter
public void setId(int id){
this.id = id;
}
public void setbookingnum(String booking_num){
this.booking_num = booking_num;
}
public void settitle(String title){
this.title = title;
}
public void setbookingstart(String booking_start){
this.booking_start = booking_start;
}
public void setbookingend(String booking_end ){
this.booking_end = booking_end ;
}
public void setpropertytype(String property_type){
this.property_type = property_type;
}
public void setcustomerid(int customer_id){
this.customer_id = customer_id;
}
public void setdescription(String description){
this.description = description;
}
public void setacceptstatus(String accept_status){
this.accept_status = accept_status;
}
public void setbookingaddress(String booking_address){
this.booking_address = booking_address;
}
public void setpobox(String po_box ){
this.po_box = po_box ;
}
public void setcreated(int created){
this.created = created ;
}
public void setpostcode(int postcode){
this.postcode = postcode;
}
public void setstate(String state){
this.state =state ;
}
public void setstreetaddress(String street_address){
this.street_address = street_address ;
}
public void setstreetnumber(int street_number){
this.street_number = street_number;
}
public void setsuburb(String suburb){
this.suburb = suburb;
}
public void setunit_lot_number(int unit_lot_number ){
this.unit_lot_number = unit_lot_number;
}
public void setstatus(String status){
this.status =status ;
}
public void setconvert_status(String convert_status){
this.convert_status =convert_status ;
}
public void setqoute(Float qoute){
this.qoute = qoute;
}
public void setsubtotal(Float sub_total ){
this.sub_total =sub_total ;
}
public void settotal_discount(Float total_discount){
this.total_discount =total_discount ;
}
public void setbookingdistance(Float booking_distance){
this.booking_distance =booking_distance ;
}
public void setgst(Float gst){
this.gst = gst;
}
public void setoriginal_booking_id(int original_booking_id){
this.original_booking_id = original_booking_id ;
}
// --- getter ---
public int getId(){
return id;
}
public String getbookingnum(){
return booking_num;
}
public String gettitle(){
return title ;
}
public String getbookingstart(){
return booking_start ;
}
public String getbooking_end(){
return booking_end;
}
public String getpropertytype(){
return property_type;
}
public int getcustomer_id(){
return customer_id ;
}
public String getdescription(){
return description;
}
public String getaccept_status(){
return accept_status;
}
public String getbookingaddress(){
return booking_address ;
}
public String getpo_box(){
return po_box;
}
public int getcreated(){
return created;
}
public int getpostcode(){
return postcode ;
}
public String getstate(){
return state ;
}
public String getstreet_address(){
return street_address ;
}
public int getstreet_number(){
return street_number ;
}
public String getsuburb(){
return suburb ;
}
public int getunit_lot_number(){
return unit_lot_number ;
}
public String getstatus(){
return status;
}
public String getconvert_status(){
return convert_status;
}
public Float getqoute(){
return qoute ;
}
public Float getsub_total(){
return sub_total;
}
public Float gettotal_discount(){
return total_discount;
}
public Float getbooking_distance(){
return booking_distance;
}
public Float getgst(){
return gst;
}
public int getoriginal_booking_id(){
return original_booking_id ;
}
public String toString(){
return "Booking >> id:"+id+" | booking_num:"+booking_num+" | title:"+title+
" | booking_start"+booking_start+" | booking_end"+booking_end+" | property_type"+property_type+
" | customer_id "+customer_id +" |description "+description+" | accept_status"+accept_status+
" | booking_address"+booking_address+" | po_box"+po_box+" | created"+created+" | postcode"+postcode+
" | state"+state+" | street_address"+street_address+" | street_number"+street_number+
" | suburb"+suburb+" | unit_lot_number"+unit_lot_number+" | status"+status+
" | convert_status"+convert_status+" | qoute"+qoute+" | sub_total"+sub_total+
" | total_discount"+total_discount+" | booking_distance"+booking_distance+" | gst"+gst+
" | original_booking_id"+original_booking_id;
}
}
allbookingfrag
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class allbookingfrag extends Fragment {
public static final String PREFS_NAME = "MyApp_Settings";
private static final String givenUsername = "email";
private static final String givenPassword = "password";
private static final String TAG_RESULT = "result";
private static final String TAG_BOOKING_NUM = "booking_num";
private static final String TAG_TITLE = "title";
private static final String TAG_STATUS = "status";
private static final String TAG_BOOKING_START = "booking_start";
private static final String TAG_BOOKING_END = "booking_end";
private static final String TAG_QOUTE = "qoute";
private static final String TAG_BOOKING_ADDRESS = "booking_address";
private static final String TAG_CUSTOMER = "customer";
private static final String TAG_CUSTOMER_NAME = "name";
String result = null;
ArrayList<HashMap<String, String>> resultList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.allbookingfrag, container, false);
SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
String email = settings.getString("email",givenUsername);
String password = settings.getString("password",givenPassword);
System.out.println("In allbooking activity names is : " + email + "password is : " + password );
connectWithHttpGet(email, password );
resultList = new ArrayList<HashMap<String, String>>();
ListView listallbooking = (ListView)rootView.findViewById(R.id.listallbooking);
return rootView;
}
private void connectWithHttpGet(String email, String password ) {
class HttpGetAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// As you can see, doInBackground has taken an Array of Strings as the argument
//We need to specifically get the givenUsername and givenPassword
String email = params[0];
String password = params[1];
// String uuid = params[2];
System.out.println("paramUsername: " + email + " paramPassword is : " + password + "paramuuid is: ");
// Create an intermediate to connect with the Internet
HttpClient httpClient = new DefaultHttpClient();
// Sending a GET request to the web page that we want
// Because of we are sending a GET request, we have to pass the values through the URL
HttpGet httpGet = new HttpGet("xxxxxxxxxx?email=" + email + "&password=" + password +"&uuid=fdgsfsdfsfsf"+"&mode=booking" );
StringBuilder stringBuilder = new StringBuilder();
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String jsonStr = null;
jsonStr = EntityUtils.toString(httpEntity);
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONObject resultObj = jsonObj.getJSONObject("result");
JSONArray resultArr = resultObj.getJSONArray("result");
//Log.d("Response: ", "> " + jsonStr);
// looping through All Contacts
for (int i = 0; i < resultArr.length(); i++) {
JSONObject c = resultArr.getJSONObject(i);
String booking_num = c.getString(TAG_BOOKING_NUM);
String title = c.getString(TAG_TITLE);
/* String splittitle= title;
String [] mysplit_title = null;
mysplit_title = splittitle.split("-");
String First_title=mysplit_title[0];
String Qoute_title=mysplit_title[1];
String Username_title=mysplit_title[2];*/
String status = c.getString(TAG_STATUS);
String booking_start = c.getString(TAG_BOOKING_START);
String my_starttime=booking_start;
String [] my_date_time_start = null;
my_date_time_start = my_starttime.split(" ");
String Date_str_start=my_date_time_start[0];
String Time_str_start=my_date_time_start[1];
String booking_end = c.getString(TAG_BOOKING_END);
String my_endtime=booking_start;
String [] my_date_time_end = null;
my_date_time_end = my_endtime.split(" ");
String Date_str_end=my_date_time_end[0];
String Time_str_end=my_date_time_end[1];
String qoute = c.getString(TAG_QOUTE);
String booking_address = c.getString(TAG_BOOKING_ADDRESS);
// System.out.println("booking_num: " + booking_num + " title is : " + title + "booking_start: "+ booking_start);
JSONObject customer = c.getJSONObject(TAG_CUSTOMER);
String name = customer.getString(TAG_CUSTOMER_NAME);
BookingTabel db = new BookingTabel(getActivity());
// tmp hashmap for single contact
HashMap<String, String> result3 = new HashMap<String, String>();
// adding each child node to HashMap key => value
result3.put(TAG_BOOKING_NUM, booking_num);
result3.put(TAG_TITLE, title);
result3.put(TAG_STATUS, status);
result3.put(TAG_BOOKING_START, Time_str_start);
result3.put(TAG_BOOKING_END, Time_str_end);
result3.put(TAG_QOUTE, qoute);
result3.put(TAG_BOOKING_ADDRESS, booking_address);
result3.put(TAG_CUSTOMER_NAME, name);
// Hashmap for ListView
// ArrayList<HashMap<String, String>> resultList = new ArrayList<HashMap<String, String>>();
// adding contact to contact list
resultList.add(result3);
System.out.println("resultList: " + resultList);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// Argument comes for this method according to the return type of the doInBackground() and
//it is the third generic type of the AsyncTask
@Override
protected void onPostExecute(String result) {
ListView listallbooking = (ListView)getActivity().findViewById(R.id.listallbooking);
ListAdapter adapter = new SimpleAdapter(getActivity(), resultList,
R.layout.list_item_allbooking,
new String[] { TAG_TITLE,TAG_STATUS, TAG_BOOKING_START,
TAG_BOOKING_END,TAG_QOUTE,TAG_BOOKING_ADDRESS,TAG_CUSTOMER_NAME }, new int[] {
R.id.listtitle,R.id.liststatus,
R.id.listbooking_start, R.id.listbooking_end,R.id.listqoute,R.id.listbooking_address,R.id.listcustomername});
listallbooking.setAdapter(adapter);
// System.out.println("resultList: " + resultList);
// setOnItemClickListener
listallbooking.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int postion,
long arg3) {
// TODO Auto-generated method stub
Intent detailesitem = new Intent(getActivity().getBaseContext(), estimates.class);
startActivity(detailesitem);
}
});
}
}
// Initialize the AsyncTask class
HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
// Parameter we pass in the execute() method is relate to the first generic type of the AsyncTask
// We are passing the connectWithHttpGet() method arguments to that
httpGetAsyncTask.execute(email, password );
}
}
最佳答案
为了更好地维护代码,我建议使用 Android Volley 库。您获得异步 API 来调用 rest API,回调直接提供您的 JSON 对象。
并使用 GSON 库将 JSON 转换为您定义的 Java 对象(预订)。您唯一需要做的就是定义与 JSON 结构中的键相同的 Bookings 成员变量名称。
现在您应该有一个实用方法可以通过从 Bookings 对象中读取值来插入到数据库中。
然后生成可维护的无错误代码。
关于android - 我应该怎么做才能将数据从 json 插入到 Android 中的 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26713555/
我正在开发一个 SQLite 数据库。数据库已经填满了,但我想重构它。这是我需要做的一个示例: 我目前有一张 table : CREATE TABLE Cars (ID INTEGER PRIMARY
我正在使用 Mono、SQLite、Dapper 和 Dapper 扩展。我可以从数据库中读取数据,但插入不起作用。我正在使用 sqlite 的 Mono 驱动程序。 错误并不能提供太多信息,至少对我
我有一个使用 SQLite 的 Windows Phone 8 应用程序。该应用程序具有许多数据库功能,并包含一个 sqlite 数据库文件,在运行该应用程序时,该文件将被复制到本地文件夹并进行访问。
为 sqlite 创建索引时有排序顺序。 https://sqlite.org/lang_createindex.html Each column name or expression can be
顾名思义,我怀疑如果有一些引用被删除的表会发生什么,例如表的某些字段的索引。 SQLite是否会自动处理?在执行drop命令之前,数据库所有者是否应注意任何实例? 最佳答案 我认为不需要家政服务。 S
我想知道是否有可能将从计数中获得的整数转换为REAL 类似于以下内容(尽管这不起作用) SELECT CAST (COUNT (ColumnA) AS Count) AS REAL) FROM Tab
我无法在SQLite数据库上执行一些更新。我正在Windows上使用SQLite 3 Shell。 我正在运行以下命令: update resovled_chrom_counts set genus
我知道SQLite中的触发器顺序是不确定的(您不能确定将首先执行哪个触发器),但是表约束和触发器之间的关系又如何呢? 我的意思是,假设我在一个列中有一个UNIQUE(或CHECK)约束,并且在该表上有
我的 CustomTags 表可能有一系列“临时”记录,其中 Tag_ID 为 0,并且 Tag_Number 将有一些五位数的值。 定期,我想清理我的 Sqlite 表以删除这些临时值。 例如,我可
我有A,B,C和D的记录。 我的SQL1 SELECT * FROM main_table order by main_table.date desc limit 2返回A和B。 我的SQL2 SEL
select round(836.0)返回836.0 我如何删除sqlite查询中的尾随零。 836.00应该是836 836.440应该是836.44 最佳答案 如果需要836.44,则需要十进制返
我正在研究RQDA中的文本,并且正在使用Firefox SQLite Manager访问数据库,以便可以更轻松地搜索文件。我创建并填充了虚拟表: CREATE VIRTUAL TABLE texts
我有这样的数据: table1 id | part | price 1 | ox900 | 100 2 | ox980 | 200 和 table2 id | part | price 1
我正在尝试将一些数据插入现有的SQLite表中。该表和数据库是使用相同的API创建的,但是由于某种原因,插入操作无效,并且从不给我任何错误消息。 我正在BlackBerry 9550模拟器上对此进行测
例如,我在名为SALARY的列中插入一个值。如果插入的值大于1000,我想将字符串HIGH插入到RANK列中,否则将插入LOW中。 我可以使用SQLite做到吗? 最佳答案 在插入之前使用触发器,然后
假设我有一个包含三列A,B,C的表t1,其中(A,B)包含唯一键(具有数十万行)。由于90%的查询将采用SELECT C FROM t1 WHERE A =?和B = ?,我想我要为A,B和C提供覆盖
在一个SQLite3数据库中,我有一个表“ projects”,其id字段由以下方式组成: [user id]_[user's project id] 例如,用户ID = 45,这是一些数据: 45_
我了解PRAGMA foreign_key和ON DELETE RESTRICT/NO ACTION的概念,但是我面临的是另一种情况。 我需要删除一个父行,但保持与之关联的子行。例如: CREATE
我的c#应用程序从Web服务1读取文件列表,并将完整的文件名插入table1,然后从第二个Web服务读取list并将它们插入到table2。 这些表具有相同的结构,如下所示: create table
我在以下情况下尝试将Record1的ID更新为Record2的ID: 两个表中的名称相同,并且 在Record2中权重更大。 记录1 | ID | Weight | Name | |----|----
我是一名优秀的程序员,十分优秀!