我将数据库中的值提取到.gitignore
,但是我想将Spinner
中的选定项插入到数据库中,我该怎么做?
我使用了两个Spinner
,并且Spinners
所选项都应该插入到数据库中。
如何在Spinner's
中插入项目以及我需要做什么?
活动类: -
PHP
答案 0 :(得分:1)
如果要在sqlite中存储微调器选定项,请遵循此代码 1.创建一个本地数据库 2.在您的活动中插入选定的值,如下所示。
LocalDatabase:
public class SpinnerDatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "spinnerModule";
// Labels table name
private static final String TABLE_LABELS = "spinner";
// Labels Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_SPINONE = "spinone";
private static final String KEY_SPINTWO = "spintwo";
SQLiteDatabase db;
public LoginDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_SPINONE + " TEXT,"
+ KEY_SPINTWO + " TEXT" + ");";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
// Create tables again
onCreate(db);
}
public void insertLabel(String spinone,String spintwo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_ID,id);
values.put(KEY_SPINONE,spinone);
values.put(KEY_SPINTWO,spintwo);
// Inserting Row
db.insert(TABLE_LABELS, null, values);
db.close(); // Closing database connection
}
}
将值从您的活动插入sqlite中 1.将主要活动中的sqlite数据库初始化为
SpinnerDatabaseHandler db=new SpinnerDatabaseHandler(getApplicationContext);
2.之后添加这些代码
String firstspin=spinner1.getSelectedItem.toString();
String secondspin=spinner2.getSelectedItem.toString();
db.insertlabel(firstspin,secondspin);
答案 1 :(得分:0)
我假设您有“保存/确定”按钮,用于将微调器选定项插入数据库。
获取微调器选定的项目值
String spinner1SlctdVal = spinner1.getSelectedItem().toString(); // Course spinner
String spinner2SlctdVal = spinner2.getSelectedItem().toString(); // Username spinner
对于PHP中的插入微调器选定项
you have to call PHP web-service using AsyncTask or other methods.
当用户点击代码下面的“保存/确定”按钮时,从两个微调器中选择值后。
保存/确定按钮代码单击:
mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String spinner1SlctdVal = spinner1.getSelectedItem().toString();
String spinner2SlctdVal = spinner2.getSelectedItem().toString();
// Insert spinner selected values in database
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("course", spinner1SlctdVal); // "course" is column name of your table
values.put("username", spinner2SlctdVal); // "username" is column name of your table
database.insert("users", null, values); // "users" is your table name
database.close();
// Call AsynTask or other method for call web-service (for insert selected values into PHP server)
}
});
答案 2 :(得分:0)
插入值的Php文件:
<?php
$_SERVER['REQUEST_METHOD']=='POST';
define('HOST','hostname');
define('USER','username');
define('PASS','password');
define('DB','databasename');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
$firstspin=$_POST['lspinone'];
$secondspin=$_POST['lspintwo'];
$query = "INSERT INTO `spinnertable` (`spione`, `spintwo`) VALUES ('".$firstspin."', '".$secondspin."')";
$inserted = @mysqli_query($con, $query);
if($inserted == 1 ){
echo "success";
}else{
echo "user exist already";
}
mysqli_close($con);
?>
将值从android传递给php的代码:
package nidhinkumar.gridcam.logindetails;
public class MainPAge extends AppCompatActivity{
private static final String LOGINALL_URL = "http://smpleuser.com/07032016/php/nameofphpfile.php";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerpage);
String spinone=spinner1.getSelectedItem.toString();
Stirng spintwo=spinner2.getSelectedItem.toString();
// Call AsyncTask for send selected value to PHP server.
userSpinnr(spinone, spintwo);
}
private void userSpinnr(final String lspinone, final String lspintwo) {
rando=(int)(Math.random()*10);
class LoginSpinners extends AsyncTask<String,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginPage.this, "Do you know?", "", true, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s.equalsIgnoreCase("success")) {
//do the action what you want
} catch (SQLException e) {
}
}else{
loading.dismiss();
} catch (SQLException e) {
}
}
}
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("lspinone",params[0]);
data.put("lspintwo",params[1]);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(LOGINALL_URL,data);
return result;
}
}
LoginSpinners ulc = new LoginSpinners();
ulc.execute(lspinone,lspintwo);
}
}