gpt4 book ai didi

android - 将json插入mysql

转载 作者:行者123 更新时间:2023-11-29 11:29:02 25 4
gpt4 key购买 nike

我正在寻找一种将 JSON 插入 SQLite 的方法。我有一个如下所示的 php 脚本。

<?php 
require 'connection.php';

$var_fname = "Mildred";
$var_password = "ewan";
$var_email = "saf";
$var_username = "enanpogi";
$var_lastname = "Mildred";
$var_aviaryName = "Mildred";
$var_location = "myLocation";

$insert_stmt = $db->prepare(""
. "INSERT INTO users"
. "( fname, lname, email, password, username, aviary_name, location ) "
. "VALUES( :name, :lastname, :email, :password, :username, :aviaryName, :location)" );
// prepare and bind
$insert_stmt->execute(array(
':name' => $var_fname,
':lastname' => $var_lastname,
':password' => $var_password,
':email' => $var_email,
':username' => $var_username,
':aviaryName' => $var_aviaryName,
':location' => $var_location
));
$lastId = $db->lastInsertId();

if ($insert_stmt){
echo "inserted</br>";
echo $lastId;
//get all information of newly inserted and make json encode to prepare for insertion in sqlite_array_query

$stmt = $db->prepare('SELECT * '
. 'FROM users '
. 'WHERE id = :lastInsertedID ');
$stmt->bindParam(':lastInsertedID', $lastId);
$stmt->execute();

$results = $stmt->fetch(PDO::FETCH_ASSOC);

if($results > 0 ){
//echo sizeof($results);
echo "</br>";
echo json_encode(array("user_data"=>$results) );
}
}
?>

当我运行该脚本时,我得到了以下结果

{"user_data":{"id":"28","fname":"Mildred","lname":"Mildred","email":"saf","username":"enanpogi","aviary_name":"Mildred","password":"ewan","location":"myLocation","phonenumber":null,"active":null}}

我不确定它是 JSON 对象还是 JSON 数组..

现在,我需要帮助将该 JSON 插入到我的 Android JAVA 应用程序中的 SQLite。

最佳答案

访问此链接:Json Viewer

  • 将您的输出作为文本粘贴到此链接中。
  • 单击“查看器”。

您可以查看是否收到 json 响应。我检查了你的结果。它是 json 格式。你已经得到了 json 对象。

现在,使用 json_encode($results) 函数为您提供一个字符串,以便您可以轻松地将其插入数据库。

关于android - 将json插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37727256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com