Android数据库处理程序

时间:2014-04-27 20:53:49

标签: android sqlite

我将从用户那里获取四个字符串,然后将它们插入数据库中。 我知道这可以使用SQLlite来完成,但我对该方法的语法感到困惑:

继承我的活动代码:

    package com.example.wheresmyspot2;

public class Store extends Activity {
GPSTracker gps;
private EditText nametxt, lattxt, lngtxt;
private Spinner spin;
private Button mapbutt, savebutt;
private String a1,a2,a3,a4;
public static final String filenam = "wms";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.storexml);

    init();
}

private void init() 
{
    gpsStuff();
    mapbutt = (Button) findViewById(R.id.MapView);   //opens a map, not related to the question
    mapbutt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showMap();
        }
    });

    savebutt = (Button) findViewById(R.id.Save);    //ought to save the data
    savebutt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveVal();
        }
    });


}

private void gpsStuff() {
    // gps stuff
    EditText lati = (EditText) findViewById(R.id.Lati);
    EditText longi = (EditText) findViewById(R.id.Longi);
    lati.setEnabled(false);
    longi.setEnabled(false);
    gps = new GPSTracker(Store.this);
    // Check if GPS enabled
    if (gps.canGetLocation()) {
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
        Toast.makeText(getApplicationContext(),"Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_SHORT).show();
        String lt = String.valueOf(latitude);
        String lg = String.valueOf(longitude);
        lati.setText(lt);
        longi.setText(lg);

    } else {

        Toast.makeText(getApplicationContext(), "Cant find Your Location",Toast.LENGTH_SHORT).show();
        gps.showSettingsAlert();
    }

}
private void showMap() 
{
    //code to show map  
}

private void saveVal() 
{
    a1=((EditText)findViewById(R.id.nick)).getText().toString();
    a2=((EditText)findViewById(R.id.Lati)).getText().toString();
    a3=((EditText)findViewById(R.id.Longi)).getText().toString();
    a4=((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString();

    //I got 3 values from text box, and one from a spinner
    /*
            have no idea where to put this code     

    String insert_data="INSERT INTO "+table_name"+" (Column_a1,Column_a2,Column_a3,Column_a4) VALUES " + "('" + a1 + "'," + "'" + a2 + "'," + "'" + a3 + "'," + "'" + a4 + "'"+")";
    shoppingListDB.execSQL(insert_data);
    */
    Toast.makeText(getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();
}

}

2 个答案:

答案 0 :(得分:0)

如果你只需要存储4个字符串,那么你应该使用SharedPreferences但是如果你真的需要使用datebase,那么我建议创建你的数据库助手类,你可以找到例如{{3}的教程}。使用SharedPreferences

//Saving data to SharedPreferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(Key1, a1);
editor.putString(Key2, a2);
editor.putString(Key3, a3);
editor.putString(Key4, a4);
editor.commit();

// to get the value back
String a1 = SharedPreferences.getString(Key1, "");
String a2 = SharedPreferences.getString(Key2, "");
String a3 = SharedPreferences.getString(Key3, "");
String a4 = SharedPreferences.getString(Key4, "");

如果要存储少量数据,使用SharedPreferences会更容易,更有效

答案 1 :(得分:0)

做这样的事情

//check if item exists before adding it to the database
                    Cursor checkIfExist = shoppingListDB.rawQuery("SELECT * FROM "+table_name+" WHERE ITEM_NAME =" +"'"+a1 +"'", null);
                    if (checkIfExist.moveToFirst()){
                        Toast.makeText(getBaseContext(), "Item Exist", Toast.LENGTH_LONG).show();
                    }//else add it
                    else{
                        String insert_data="INSERT INTO "+table_name+" (Column_a1,Column_a2,Column_a3,Column_a4) VALUES " + "('" + a1 + "'," + "'" + a2 + "'," + "'" + a3 + "'," + "'" + a4 + "'"+")";
                        shoppingListDB.execSQL(insert_data);
                        Toast.makeText(getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();
                    }