0
我將從用戶獲取四個字符串,然後將它們插入到數據庫中。 我知道這是可以使用SQLlite做,但我有一個關於該方法的語法混亂:Android數據庫處理程序
繼承人我activiy代碼:
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();
}
}
你應該先了解SQL,[點擊這裏](http://developer.android.com/training/basics/data-storage/databases.html)是關於SQL數據庫的官方指南。 –