简体   繁体   English

我可以在浏览器中使用数据库吗?

[英]Can I use a database in the browser?

My application needs to use a database. 我的应用程序需要使用数据库。 I've read online that databases are supported in some browsers but they are now deprecated? 我已经在线阅读了某些浏览器支持的数据库,但是现在不赞成使用它们吗? It was very confusing. 这非常令人困惑。 I need to use a database with HTML. 我需要使用HTML数据库。 Is it possible to use a database with HTML5? 是否可以在HTML5中使用数据库?

UPDATE 更新
It needs a database to store data when the user is offline. 当用户离线时,它需要一个数据库来存储数据。 It needs to support the major browsers from IE 9 on up if possible on Mac and Win on desktop and possibly Linux. 如果可能的话,它需要支持IE 9及更高版本的主要浏览器(在Mac上),在桌面和Linux上则可以支持Win。 The application is a client side HTML editor. 该应用程序是客户端HTML编辑器。 There is no server side yet. 还没有服务器端。 So I need to persistently store "files" on the client side. 因此,我需要在客户端永久存储“文件”。 Also, when I do have a server side I still need to save the users work when they have no internet connection. 另外,当我有服务器端时,当他们没有Internet连接时,我仍然需要保存用户的工作。 Later, when they go online the application can sync up with the server side. 稍后,当他们联机时,应用程序可以与服务器端同步。 I could make do with Local storage but it is too limited in space at 5MB. 我可以使用本地存储,但是它的空间太有限,只有5MB。 That's the maximum limit? 那是最大限制? The application allows you to use images and those will be saved to base 64 data URI resources so you will quickly use up that space with just a few projects. 该应用程序允许您使用图像,这些图像将被保存到基于64个数据URI的资源中,因此您仅需几个项目即可快速使用该空间。

A few people asked why go for a database on the client. 一些人问为什么要在客户端上建立数据库。 Because it's appropriate for this situation and if browsers support it then I would like to use it. 因为它适合这种情况,并且如果浏览器支持它,那么我想使用它。

You should not use a database in the browser, even if you could, because that probably is not standard conforming. 即使可以,也不要在浏览器中使用数据库,因为这可能不符合标准。 However perhaps consider HTML5 local storage 但是,也许考虑使用HTML5本地存储

What you can is develop some web service accessing a database; 您可以做的是开发一些访问数据库的Web服务。 that access happens in the HTTP server (so the database is used in the server, not in the browser, which just indirectly gives access to it). 访问发生在HTTP服务器中(因此数据库是在服务器中使用的,而不是在浏览器中使用的,而浏览器只是间接地提供对它的访问)。

I would suggest to develop your web application in Opa but you could even use PHP for that. 我建议在Opa中开发Web应用程序,但您甚至可以使用PHP。 And you could make it a CGI or a FastCGI application. 您可以使其成为CGI或FastCGI应用程序。

BTW; BTW; you did not tell us which operating system you are using. 您没有告诉我们您使用的是哪个操作系统。

You could consider making your application some specialized HTTP server, eg using Wt or Ocsigen -or using some HTTP server library like libonion etc- and then it could access some database (and your application could run on eg localhost:8086 if you wanted to). 您可以考虑使您的应用程序成为某些专用的HTTP服务器,例如使用WtOcsigen-或使用某些HTTP服务器库(如libonion等),然后它可以访问某些数据库(如果需要,您的应用程序可以在localhost:8086上运行) 。

PS. PS。 I believe that having some web pages storing a large amount of data on the client side (in the browser) is simply not reasonable (and against the standard and the spirit of the web). 我认为,让某些网页在客户端(在浏览器中)存储大量数据是不合理的(这违背了网络的标准和精神)。 You should store such large data on the server side (or have some URL to download it from the server). 您应该将如此大的数据存储在服务器端(或具有一些URL从服务器下载)。 You could decide to make a "server" application running on localhost (of course, this is operating system specific). 您可以决定在localhost运行“服务器”应用程序(当然,这是特定于操作系统的)。

PPS: If coding an HTML editor, I am not sure that standard browsers are the adequate tool. PPS:如果编写HTML编辑器,我不确定标准的浏览器是否合适。 You may however use the contenteditable attribute. 但是,您可以使用contenteditable属性。

Check in CanIUse website. 登录CanIUse网站。

http://caniuse.com/#feat=sql-storage http://caniuse.com/#feat=sql-storage

It's not DEPRECATED, but it's not supported by all browsers so far. 它不是已弃用,但到目前为止,并非所有浏览器都支持。 The reason is that it did not become a valid standard because all the approaches are based in SQLite, and this is way too limited to be considered a standard. 原因是它没有成为有效的标准,因为所有方法都基于SQLite,并且这种方法过于局限,无法被视为标准。

yes go for indexedDB. 是的,请使用indexedDB。 Its still in development phase but you can use it 它仍处于开发阶段,但您可以使用它

http://www.html5rocks.com/en/tutorials/indexeddb/todo/

I've read online that databases are supported in some browsers but they are now deprecated? 我已经在线阅读了某些浏览器支持的数据库,但是现在不赞成使用它们吗?

You're right: The Web SQL Database spec has been abandoned. 您说对了: Web SQL数据库规范已被放弃。 There are a few browsers that still support it, but not many, and it will likely disappear entirely in the very near future. 有一些浏览器仍然支持它,但是不支持它,并且在不久的将来它可能会完全消失。

As an alternative, a newer spec called IndexedDB is now being promoted by the browser vendors. 作为替代方案,浏览器供应商现在正在推广一种名为IndexedDB的较新规范。 The down-side is that it is still relatively new, and thus you will probably have many users with have browsers that do not support it. 不利的一面是它仍然是相对较新的,因此您可能会吸引许多用户使用不支持它的浏览器。

Depending on the size of the data you need to store, you might consider Web Storage instead. 根据需要存储的数据大小,可以考虑使用Web存储 This is intended just for plain text, and can't store massive quantites of data, but it is a well-established standard, so you've got a much better chance of compatibility. 这仅适用于纯文本,不能存储大量数据,但这是一个公认的标准,因此您有更大的兼容性机会。

If you can get away with storing a relatively small data set (ie a few megabytes or less) then this is definitely the best option here. 如果您可以摆脱存储相对较小的数据集(即几兆字节或更小)的问题,那么绝对是这里的最佳选择。 This would certainly be my advice; 这肯定是我的建议; try to keep your offline data requirements as small as possible, and keep them in Web Storage. 尽量使您的离线数据需求尽可能小,并将其保留在Web存储中。

Hope that helps. 希望能有所帮助。

For a detailed run-down of all available browser-side storage options, you should read this article . 要详细了解所有可用的浏览器端存储选项,请阅读本文 It gives a much more detailed description of each of the options I described above, plus a few others. 它提供了我上面描述的每个选项以及其他几个选项的详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM