我创建了一个在线数据库,包含一个包含四行的表,KEY_ID,KEY_SLOT_NAME,KEY_AVAILABLE_SLOT和KEY_TOTAL_SLOT。
每个行都有自己的行并分配了默认值,只有KEY_AVAILABLE_SLOT的值会更改。
我已经创建了JSONFunctions类
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
从How to connect Android app to MySQL database?
开始现在我陷入了第四步,即阅读JSON标签,因为我不明白它是什么。
以下是我使用脱机数据库进行测试时的示例代码用法:
final TextView parkAvaliableA = (TextView) findViewById(R.id.parkASlot);
parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Button buttonA = (Button) findViewById(R.id.parkABtn);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(SlotA.available_slot == 0)
{
Toast.makeText(getApplicationContext(),
"Parking slot A is full",
Toast.LENGTH_SHORT).show();
}
else
{
SlotA.available_slot -= 1;
db.updateSlot(new ParkingSlot());
}
//parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Intent myIntent = new Intent(MainMap.this, ParkA.class);
MainMap.this.startActivity(myIntent);
}
});
我需要对我的数据库进行基本的更新操作,以便每按一次按钮就可以更新KEY_SLOT_AVAILABLE值。
还有一个OOT问题,如果我可以:“我应该在每个数据库列中编写KEY_吗?或者只是一个麻烦?当我阅读一些在线教程时,我开始使用KEY_使用KEY _”
答案 0 :(得分:1)
您需要编写如下所示的脚本以连接到MySql数据库:
<?php
$servername = "Your_Server_Name";
$username = "Your_UserName";
$password = "Your_Password";
$dbname = "Your_DB_Name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
成功连接到数据库后,您可以使用服务器执行所有数据库操作。
检查以下链接,您将获得一个想法:
http://www.w3schools.com/php/php_mysql_create.asp
http://www.tutorialspoint.com/php/create_mysql_database_using_php.htm
希望这有帮助。