如何将 Android 连接到数据库服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3419697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:07:18  来源:igfitidea点击:

How to connect Android to a database server

androiddatabase-connection

提问by Alex

Is there any sample? I have my android application and I need to connect to mysql server on my machine, what is the best way?

有样品吗?我有我的 android 应用程序,我需要连接到我机器上的 mysql 服务器,最好的方法是什么?

I should not use jdbc, explanation here

我不应该使用jdbc,解释在这里

Never never use a database driver across an Internet connection, for any database, for any platform, for any client, anywhere. That goes double for mobile. Database drivers are designed for LAN operations and are not designed for flaky/intermittent connections or high latency.

永远不要通过 Internet 连接、任何数据库、任何平台、任何客户端、任何地方使用数据库驱动程序。这对移动设备来说翻了一番。数据库驱动程序专为 LAN 操作而设计,不适用于不稳定/间歇性连接或高延迟。

And should go for:

并且应该去:

DefaultHttpClient httpclient = new DefaultHttpClient(); 

But there is no example in how to open a connection or execute a simple sql statement. anyone could help me?

但是没有关于如何打开连接或执行简单 sql 语句的示例。任何人都可以帮助我吗?

采纳答案by Josh Bolton

You should either use web services or implement an HTTP handler and transfer in a RESTful manner.

您应该使用 Web 服务或实现 HTTP 处理程序并以 RESTful 方式进行传输。

回答by Rajesh Kumar

I tried to connect DatabaseServer from an Android Application,initially i faced some issue while i was using jtds, jar package for database Driver Support, instead of using jtds jar file use mysql-Connector jar file for Database Driver Support. mysql-connector jar file "mysql-connector-java-5.1.24-bin.jar". put this jar file in projects libs folder.

我尝试从 Android 应用程序连接 DatabaseServer,最初我在使用 jtds 时遇到了一些问题,jar 包用于数据库驱动程序支持,而不是使用 jtds jar 文件,使用 mysql-Connector jar 文件进行数据库驱动程序支持。mysql-connector jar 文件“mysql-connector-java-5.1.24-bin.jar”。将此 jar 文件放在项目 libs 文件夹中。

a little bit code snippet :

一点代码片段:

`String url="jdbc:mysql://(IP-of databaseServer):3306/DBNAME";`
`String driver="org.gjt.mm.mysql.Driver";`
`String username="XYZ";//user must have read-write permission to Database
`String password= "*********";`//user password

`try{
    Class.forName(driver).newInstance();//loading driver
    Connection con = DriverManager.getConnection(url,username,password);
    /*once we get connection we can execute ths SQL command to Remote Database Server with the help mysql-connector driver over Internet*/

}`    
`catch(Exception e){
    e.printStackTrace();
}`

I hope it could work.

我希望它可以工作。

Cheers Rajesh P Yadav.

干杯拉杰什·P·亚达夫。

回答by Neil McGuigan

I'm a fan of Offline First, meaning your app should be usable without a connection.

我是Offline First的粉丝,这意味着您的应用程序应该可以在没有连接的情况下使用。

To facilitate this, I would recommend using a SQLite local database, and syncing to a remote database when online.

为此,我建议使用 SQLite 本地数据库,并在联机时同步到远程数据库。

You can use a tool like SymmetricDSor Daffodil Replicatorto sync your local and remote databases over HTTP(S).

您可以使用SymmetricDSDaffodil Replicator等工具通过 HTTP(S) 同步您的本地和远程数据库。

回答by EboMike

In order to connect to a MySQL server, you need a MySQL client. Android does not come with any MySQL libraries. You may be able to take a generic Java MySQL library and fudge it to work with Android, but that would be a big undertaking and wasted time.

为了连接到 MySQL 服务器,您需要一个 MySQL 客户端。Android 没有附带任何 MySQL 库。您也许可以采用通用的 Java MySQL 库并捏造它与 Android 一起使用,但这将是一项艰巨的任务,而且会浪费时间。

The link you pointed to already told you that what you're trying to do is wrong in the first place. Don't connect to a database across the internet! You will need something on your server that responds to HTTP requests, looks up data in the database, and sends them back via HTTP. The link already mentioned a few options. You could even write something yourself, although it's most likely easier to use an existing solution than trying to make your own approach safe and hack-proof.

您指向的链接已经告诉您,您尝试做的事情首先是错误的。不要通过互联网连接到数据库!你的服务器上需要一些东西来响应 HTTP 请求,在数据库中查找数据,然后通过 HTTP 将它们发回。该链接已经提到了一些选项。您甚至可以自己编写一些东西,尽管使用现有的解决方案很可能比尝试使您自己的方法安全和防黑客更容易。

回答by Octoberdan

If you need to share data between multiple phones, I would recommend exposing it as a web service. If the data only needs to be accessible on that particular phone (and you want to remain relational) you can use sqlite.

如果您需要在多部手机之间共享数据,我建议将其公开为 Web 服务。如果仅需要在该特定手机上访问数据(并且您希望保持相关性),则可以使用 sqlite。

回答by LizB

You won't be able to connect directly to a MySQL database with the HttpClient, as the MySQL database doesn't operate on that protocol.

您将无法使用 HttpClient 直接连接到 MySQL 数据库,因为 MySQL 数据库不在该协议上运行。

The second part of the answer to the question you linked recommends going with web services and consuming those to communicate with the database server. Which is exactly what you would be able to do with the HttpClient.

您链接的问题的答案的第二部分建议使用 Web 服务并使用这些服务与数据库服务器进行通信。这正是您可以使用 HttpClient 执行的操作。