php - 具有外键支持的 SQLite Android 表和上传到远程服务器不起作用

标签 php android mysql sql sqlite

我正在尝试将 Android 中一个 SQLite 数据库的两个不同表中的一些数据上传到 MySQL 服务器上。

当我有一张表时,它上传得很好,但现在根本没有上传任何内容。我的挑战是让外键关系发挥作用,然后将两个表中的数据上传到服务器上。

以下是我到目前为止所尝试过的。第一个类名为 ResponseDetails,具有自己的创建语句:

public class ResponseDetailTable {


// table response_detail tags
  public static final String TABLE_RESPONSE_DETAIL = "response_detail";
  public static final String KEY_ID = "_id";
  public static final String KEY_ANSWER_TEXT = "answer_text";
  public static final String KEY_QUESTION_ID = "question_id";
  public static final String KEY_ANSWER_ID = "answer_id";
  public static final String KEY_RESPONSE_ID = "response_id";


  //table response tags
  public static final String TABLE_RESPONSE = "response"; 

  public static final String KEY_COL_ID = "_id";



// Database creation SQL statement for response_detail table
  private static final String CREATE_TABLE_RESPONSE_DETAIL = "create table " 
      + TABLE_RESPONSE_DETAIL
      + "(" 
      + KEY_ID + " integer primary key autoincrement, " 
      + KEY_QUESTION_ID + " integer,"
      + KEY_ANSWER_TEXT + " text not null, " 
      + KEY_ANSWER_ID + " integer ," 
      + KEY_RESPONSE_ID + " integer ,"
      + " FOREIGN KEY ("+KEY_RESPONSE_ID+") REFERENCES "+TABLE_RESPONSE+" ("+KEY_COL_ID+") "
      + ");";

第二个类名为 ResponseTable 及其创建语句:

public class ResponseTable {

// table response tags

      public static final String TABLE_RESPONSE = "response"; 
      public static final String KEY_COL_ID = "_id";
      public static final String KEY_FIRST_NAME = "firstName";
      public static final String KEY_LAST_NAME = "lastName";
      public static final String KEY_EMAIL = "email";
      public static final String KEY_PHONE_NUMBER = "phoneNumber";
      public static final String KEY_DATE = "date";



    // Database creation SQL statement for response table
      private static final String CREATE_TABLE_RESPONSE = "create table " 
          + TABLE_RESPONSE
          + "(" 
          + KEY_COL_ID + " integer primary key autoincrement, " 
          + KEY_FIRST_NAME + " text not null, " 
          + KEY_LAST_NAME + " text not null, "
          + KEY_EMAIL + " text not null, "
          + KEY_PHONE_NUMBER + " text not null, "
          + KEY_DATE + " varchar "
          + ");";

最后是我的 php 脚本:

<?php


$json = file_get_contents('php://input');
$obj = json_decode($json);


//create connection

$host = "localhost";
$user = "root";
$pwd = "";
$db = "webservice";

//create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection

if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL :" . mysqli_connect_error());

}else { echo "Connection was ok!" ;}

        foreach ($obj as $id => $jsons) {

        $query = "INSERT INTO response_detail(question_id, answer_text,answer_id, response_id ) VALUES ('".$jsons->{'question_id'}"', '".$jsons->{'answer_text'}"', '".$jsons->{'answer_id'}"','".$jsons->{'response_id'}"')";



        mysqli_query($con, $query); 


        //$sql = "INSERT INTO response(firstName, lastName, email, PhoneNumber, date) VALUES (".$jsons->firstName.",".$jsons->lastName.",".$jsons->email.",".$jsons->phoneNumber.",".$jsons->date.")";


        //mysqli_query($con, $sql); 

            }

mysqli_close($con);


//$post = $array(1);
header('COntent-type: application/json');
echo json_encode(array('post'=>$post));



?>

我的带有 PRAGMA COMMAND 的数据库助手类如下所示:

public class DatabaseHandler extends SQLiteOpenHelper{


private static String DB_PATH = "/data/data/com.mabongar.survey/databases/";

 private static final String DATABASE_NAME = "responsetable.db";
  private static final int DATABASE_VERSION = 1;
  public SQLiteDatabase myDataBase;
  private Context mycontext;


  public DatabaseHandler(Context context) throws IOException {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);



  }

  // Method is called during creation of the database
  @Override
  public void onCreate(SQLiteDatabase database) {
    ResponseDetailTable.onCreate(database);
    ResponseTable.onCreate(database);
  }



// Method is called during an upgrade of the database,
  // e.g. if you increase the database version
  @Override
  public void onUpgrade(SQLiteDatabase database, int oldVersion,
      int newVersion) {
    ResponseDetailTable.onUpgrade(database, oldVersion, newVersion);

    ResponseTable.onUpgrade(database, oldVersion, newVersion);
  }

  private boolean checkdatabase() {
        //SQLiteDatabase checkdb = null;
        boolean checkdb = false;
        try {
            String myPath = DB_PATH + DATABASE_NAME;
            File dbfile = new File(myPath);
            //checkdb =     SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
            checkdb = dbfile.exists();
        } catch(SQLiteException e) {
            System.out.println("Database doesn't exist");
        }
        return checkdb;
    }

  public void opendatabase() throws SQLException {
        //Open the database
        String mypath = DB_PATH + DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);

        if (!myDataBase.isReadOnly()) {
            // Enable foreign key constraints
            myDataBase.execSQL("PRAGMA foreign_keys=ON;");
            }

  }

  public void createdatabase() throws IOException {
        boolean dbexist = checkdatabase();
        if(dbexist) {

        } else {
             this.getReadableDatabase();

         copydatabase();
        }
    }   

  private void copydatabase() throws IOException {
        //Open your local db as the input stream
        InputStream myinput = mycontext.getAssets().open(DATABASE_NAME);

        // Path to the just created empty db
        String outfilename = DB_PATH + DATABASE_NAME;

        //Open the empty db as the output stream
        OutputStream myoutput = new FileOutputStream("/data/data/com.mabongar.survey/databases/responsetable.db");

        // transfer byte to inputfile to outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer))>0) {
            myoutput.write(buffer,0,length);
        }

        //Close the streams
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }

}

所以不起作用的是:

  1. 当我在 response_detail 表上执行 select everything 查询时,外键列永远不会显示。但是,当我删除 FK 约束并仅将其用作普通列并添加值时,它显示。
  2. 没有任何 SQLite 数据上传到我的 MySQL 服务器。我不知道如何为两个表的每个循环编写嵌套。

编辑:

第一个表 - ResponseDetail 类 包含从测验中插入的数据,其中从服务器下载的每个问题都显示在带有 View 分页器的 fragment 上。

代码真的很长,所以我只展示了当您点击“下一步”按钮时发送响应的部分。

btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (question.getWidgetId() == 1) {

                rg = (RadioGroup) relative1.findViewById(R.id.radioGrp1);

                if (rg.getCheckedRadioButtonId() != -1) {




                } else {
                    Toast.makeText(c2, "Please Select Answer", 0).show();
                }


            }else if (question.getWidgetId() == 2) {

                spinner = (Spinner) relative1.findViewById(R.id.spinner1);

                //get spinner selected value
                String spinner_text = spinner.getSelectedItem().toString();

                question_id = (Integer) question_txt.getTag();
                answer_id = (Integer) spinner.getTag();
                //get question_id for this widget

                que_id = String.valueOf(question_id);



                //get answer_id for this widget
                ans_id = String.valueOf(answer_id);


                // send values to db
                ContentValues values = new ContentValues();

                values.put(ResponseDetailTable.KEY_ANSWER_TEXT, spinner_text);

                values.put(ResponseDetailTable.KEY_QUESTION_ID, que_id);

                values.put(ResponseDetailTable.KEY_ANSWER_ID,
                         ans_id);

                getActivity()
                        .getApplicationContext()
                        .getContentResolver()
                        .insert(ResponseContentProvider.CONTENT_URI, values);

            } 
            mViewPager.setCurrentItem(getItem(+1), true);
        }

    });

编辑:

另一个类 ResponseTable 从单独 Activity 的用户联系表单中填充。

public class FormActivity extends ActionBarActivity implements OnClickListener{
private Button btnDone;
private EditText cfName, clName,cEmail, cPhoneNum;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form_activity);
cfName = (EditText) findViewById(R.id.et_fName);
        clName = (EditText) findViewById(R.id.et_lName);
        cEmail = (EditText) findViewById(R.id.et_Email);
        cPhoneNum = (EditText) findViewById(R.id.et_Phone);

}
@Override
public void onClick(View v) {

     switch (v.getId()) {


        case R.id.btnDone1:

            boolean sent = true;

            try {
            String user_fname = cfName.getText().toString();
            String user_lname = clName.getText().toString();
            String user_email = cEmail.getText().toString();
            String user_phoneNum = cPhoneNum.getText().toString();
            String date = txtDate.getText().toString();

            ContentValues cv = new ContentValues();

            cv.put(ResponseTable.KEY_FIRST_NAME, user_fname);
            cv.put(ResponseTable.KEY_LAST_NAME, user_lname);
            cv.put(ResponseTable.KEY_EMAIL, user_email);
            cv.put(ResponseTable.KEY_PHONE_NUMBER, user_phoneNum);
            cv.put(ResponseTable.KEY_DATE, date);


            getApplicationContext()
            .getContentResolver()
            .insert(ResponseContentProvider.CONTENT_URI1,
                    cv);    



            } catch (Exception e) {
                sent = false;
            } finally {
                if (sent) {

                    Toast.makeText(getApplicationContext(), "saved !",Toast.LENGTH_SHORT ).show();

                }
            }

          //  new   UserContacts().execute("http://10.0.2.2/webservice/post_data.php");

            Intent intent = new Intent (this, SubmitActivity.class);
            startActivity(intent);
           // FormActivity.this.finish();

            break;

     }

}

我基本上试图将每个用户与其响应(response_details)链接起来。所以我试图从ResponseDetailTable获取FK“response_id”来引用另一个ResponseTable(用户联系人表)的主键

最佳答案

将其放在 dbhelper(Sqlite 帮助程序)的创建中

db.execSQL("PRAGMA foreign_keys=ON;");

关于php - 具有外键支持的 SQLite Android 表和上传到远程服务器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997691/

相关文章:

PHP:如何访问已在其外部声明的函数内部的变量?

php - Cron 作业在 cpanel 中无法正常工作

php - 从ajax获取数据并发送到mysql

php - 用户数据库 - 有效和无效请求

java - 期望 removeOnTabSelectedListener 表达式?

java - 为什么旋转会导致Android fragment 替换失败?

android - 在 ListView 中播放 youtube 视频列表

mysql - 管理sql数据库中的位置的最佳方法是什么

MySql 查询从日期或至今存在的选择数据

php - 如何在 Android Studio 中将 MySQL 数据存储在变量中?