将数据从MySQL迁移到Firebase [英] Migrating data into Firebase from MySQL
问题描述
我有一个现有的PHP / MySQL应用程序,我试图迁移到AngularJS / Firebase,只是作为学习这些新技术的一种方式。
在MySQL中它自己的表格模式。一个这样的表看起来像这样:
pre $ CREATE TABLE`字典`(
`id` int(11)unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(50)COLLATE utf8_unicode_ci DEFAULT NULL,
`wordmeaning` varchar(255)COLLATE utf8_unicode_ci DEFAULT NULL,
`wordlength` int(11)unsigned NOT NULL,
PRIMARY KEY(`id`)
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
我的问题是:如何将此表结构及其中的数据从MySQL迁移到Firebase?
我尝试通过查询将数据导出到JSON字符串中,例如:
SELECT CONCAT([,
GROUP_CONCAT(
CONCAT({id:',id,',),
CONCAT( (word,word,word,word,
)
,])
AS json FROM字典;
这给出了一个有效的JSON字符串,如:
[{id:'1',单词:'cat',wordmeaning:'哺乳动物物种',字长:'3'},
{id:'2', word:投射器,wordmeaning:投掷器,字长:'8'},
{id:'3',word:'cart',wordmeaning:'东西可以携带东西',wordlength:' 4'}]
我将它保存在一个文件中,并尝试使用导入JSON按钮从Firebase导入文件,得到:解析JSON数据时出错。请验证您的输入。 (JSON字符串是有效的,我在 http://jsonviewer.stack.hu/ 检查过) p>
有关我可能做错什么的想法?我假设Firebase可以处理这些结构和数据。
谢谢。
您的JSON无效。将其更改为有效:
[{id:1,word:cat, wordmeaning:哺乳动物物种,wordlength:3},
{id:2,word:弹射器,wordmeaning wordlength:8},
{id:3,word:cart,wordmeaning:东西可以携带的东西,wordlength:4
所以这些是变化:
- 使用双引号而不是单引号
- 在键的周围放置(双引号),而不仅仅是值
- use double-quotes instead of single-quotes
- put (double) quotes around the keys, not just the values
<你可以考虑不要引用
id
和 wordlength $ c $的值c>,因为这些属性似乎是数字。
编辑
这些两个在线工具似乎正确地验证了JSON(或者至少符合Firebase的预期):
第二个也很漂亮地打印JSON,所以这可能是选择一个或另一个的理由。
I have an existing PHP/MySQL app which I am trying to migrate to AngularJS/Firebase, just as a way to learn these newer technologies.
The app has its own schema of tables in MySQL. One such table looks something like:
CREATE TABLE `dictionary` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`wordmeaning` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`wordlength` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
My question is: how do I migrate this table structure, and the data in it, from MySQL to Firebase?
I have tried exporting the data into a JSON string via a query such as:
SELECT CONCAT("[",
GROUP_CONCAT(
CONCAT("{id:'",id,"',"),
CONCAT("word:'",word,"',"),
CONCAT("wordmeaning:'",wordmeaning,"',"),
CONCAT("wordlength:'",wordlength,"'}")
)
,"]")
AS json FROM dictionary;
This gives a valid JSON string such as:
[{id:'1',word:'cat',wordmeaning:'a mammal species',wordlength:'3'},
{id:'2',word:'catapult',wordmeaning:'throwing device',wordlength:'8'},
{id:'3',word:'cart',wordmeaning:'something to carry things in',wordlength:'4'}]
I saved this in a file and tried to import the file from Firebase, using the Import JSON button, to which I got:Error parsing JSON data. Please validate your input. (The JSON string is valid. I checked it at http://jsonviewer.stack.hu/)
Any ideas about what I could be doing wrong? I am assuming that Firebase can handle such structures and the data in them.
Thanks.
解决方案 Your JSON is not valid. Change it to this to be valid:
[{"id":"1","word":"cat","wordmeaning":"a mammal species","wordlength":"3"},
{"id":"2","word":"catapult","wordmeaning":"throwing device","wordlength":"8"},
{"id":"3","word":"cart","wordmeaning":"something to carry things in","wordlength":"4"}]
So these are the changes:
You may want to consider not quoting the values of id
and wordlength
, since these properties seem to be numeric.
Edit
These two online tools seem to validate the JSON correctly (or at least in line with what Firebase expects):
The second one also pretty prints the JSON, so that might be a reason to prefer one or the other.
这篇关于将数据从MySQL迁移到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!