将数据从MySQL迁移到Firebase

编程入门 行业动态 更新时间:2024-10-23 05:42:18
本文介绍了将数据从MySQL迁移到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个现有的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字符串是有效的,我在 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

所以这些是变化:

  • 使用双引号而不是单引号
  • 在键的周围放置(双引号),而不仅仅是值
  • <你可以考虑不要引用 id 和 wordlength ,因为这些属性似乎是数字。

    编辑

    这些两个在线工具似乎正确地验证了JSON(或者至少符合Firebase的预期):

    • www.jslint/
    • HTTP:// jsonlint。 com /

    第二个也很漂亮地打印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 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:

  • use double-quotes instead of single-quotes
  • put (double) quotes around the keys, not just the values
  • 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):

    • www.jslint/
    • jsonlint/

    The second one also pretty prints the JSON, so that might be a reason to prefer one or the other.

    更多推荐

    将数据从MySQL迁移到Firebase

    本文发布于:2023-10-23 13:59:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1521025.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据   MySQL   Firebase

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!