2014-04-27 47 views
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(); 
} 

}

+0

你應該先了解SQL,[點擊這裏](http://developer.android.com/training/basics/data-storage/databases.html)是關於SQL數據庫的官方指南。 –

回答

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(); 
        } 
0

如果你需要存儲僅4字符串,那麼你應該寧願使用SharedPreferences但是,如果你真的需要使用數據庫,那麼我建議創建你的數據庫輔助類,你可以找到教程,例如here。使用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是方式更方便,更有效的,如果你存儲數據