1、创建MySQL2Sqlite脚本mysql2sqlite.sh:(代码地址:https://gist.github.com/esperlu/943776

  1. #!/bin/sh
  2.  
  3. # Converts a mysqldump file into a Sqlite compatible file. It also extracts the MySQL `KEY xxxxx` from the
  4. # CREATE block and create them in separate commands _after_ all the INSERTs.
  5.  
  6. # Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
  7. # The mysqldump file is traversed only once.
  8.  
  9. # Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
  10. # Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite
  11.  
  12. # Thanks to and @artemyk and @gkuenning for their nice tweaks.
  13.  
  14. mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \
  15.  
  16. awk '
  17.  
  18. BEGIN {
  19. FS=",$"
  20. print "PRAGMA synchronous = OFF;"
  21. print "PRAGMA journal_mode = MEMORY;"
  22. print "BEGIN TRANSACTION;"
  23. }
  24.  
  25. # CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
  26. /^\/\*.*CREATE.*TRIGGER/ {
  27. gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
  28. print
  29. inTrigger =
  30. next
  31. }
  32.  
  33. # The end of CREATE TRIGGER has a stray comment terminator
  34. /END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
  35.  
  36. # The rest of triggers just get passed through
  37. inTrigger != { print; next }
  38.  
  39. # Skip other comments
  40. /^\/\*/ { next }
  41.  
  42. # Print all `INSERT` lines. The single quotes are protected by another single quote.
  43. /INSERT/ {
  44. gsub( /\\\/, "\047\047" )
  45. gsub(/\\n/, "\n")
  46. gsub(/\\r/, "\r")
  47. gsub(/\\"/, "\"")
  48. gsub(/\\\\/, "\\")
  49. gsub(/\\\/, "\032")
  50. print
  51. next
  52. }
  53.  
  54. # Print the `CREATE` line as is and capture the table name.
  55. /^CREATE/ {
  56. print
  57. if ( match( $, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
  58. }
  59.  
  60. # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
  61. /^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) }
  62.  
  63. # Get rid of field lengths in KEY lines
  64. / KEY/ { gsub(/\([-]+\)/, "") }
  65.  
  66. # Print all fields definition lines except the `KEY` lines.
  67. /^ / && !/^( KEY|\);)/ {
  68. gsub( /AUTO_INCREMENT|auto_increment/, "" )
  69. gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
  70. gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
  71. gsub( /(COLLATE|collate) [^ ]+ /, "" )
  72. gsub(/(ENUM|enum)[^)]+\)/, "text ")
  73. gsub(/(SET|set)\([^)]+\)/, "text ")
  74. gsub(/UNSIGNED|unsigned/, "")
  75. if (prev) print prev ","
  76. prev = $
  77. }
  78.  
  79. # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
  80. # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
  81. # avoid a sqlite error for duplicate index name.
  82. /^( KEY|\);)/ {
  83. if (prev) print prev
  84. prev=""
  85. if ($ == ");"){
  86. print
  87. } else {
  88. if ( match( $, /\"[^"]+/ ) ) indexName = substr( $, RSTART+, RLENGTH- )
  89. if ( match( $, /\([^()]+/ ) ) indexKey = substr( $, RSTART+, RLENGTH- )
  90. key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
  91. }
  92. }
  93.  
  94. # Print all `KEY` creation lines.
  95. END {
  96. for (table in key) printf key[table]
  97. print "END TRANSACTION;"
  98. }
  99. '
  100. exit

2、创建convertDB.sh来调用mysql2sqlite.sh:

  1. rm -rf sqlite_db_name
  2. sh mysql2sqlite.sh -umysql_user_name -pmysql_user_pwd -hmysql_host mysql_db_name | sqlite3 sqlite_db_name
  1. 修改相应的参数:

 sqlite_db_name (要转换成的sqlite数据库名称)

  1. mysql_user_name mysql用户名称)
  1. mysql_user_pwd mysql用户密码)
  1. mysql_host mysql服务器ip地址)
  1. mysql_db_name(要转换的mysql数据库名称)

3、在Unity3d中执行shell脚本(放在Editor文件夹下):

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Diagnostics;
  6.  
  7. public class ChangeDB {
  8. static string sqliteDBpath = Application.streamingAssetsPath + "/dbFile/";
  9. static string sqliteDBname = "sqlite_db_name.db";
  10. [MenuItem("Tools/Mysql to Sqlite")]
  11. public static void mysqlToSqlite ()
  12. {
  13. if(File.Exists(sqliteDBpath)){
  14. UnityEngine.Debug.Log("delete old sqlite db file...");
  15. File.Delete(sqliteDBpath+sqliteDBname);
  16. }
  17. if(File.Exists(sqliteDBpath+sqliteDBname+".meta")){
  18. File.Delete(sqliteDBpath+sqliteDBname+".meta");
  19. }
  20. //start to convert mysql to sql
  21. UnityEngine.Debug.Log("start convert...");
  22. ProcessStartInfo proc = new ProcessStartInfo();
  23. proc.WorkingDirectory = sqliteDBpath;
  24. proc.FileName = "sh";
  25. proc.Arguments = "convertDB.sh";
  26. proc.UseShellExecute = false;
  27. proc.Verb = "";
  28. Process.Start(proc);
  29. }
  30. }

4、点击Tools下的 Mysql to Sqlite 就可以转换了

5、注意在iOS下可以将sqlite数据库放在 Application.streamingAssetsPath 目录下直接访问

在Android下访问Application.streamingAssetsPath下的文件必须先调用下面的方法解压一下:

  1. string sqlitedbPath = Application.streamingAssetsPath + "/dbFile/dbname.db";
  2.  
  3. string filepath = Application.persistentDataPath + "/dbname.db";
  4.  
  5. IEnumerator LoadSqliteDBForAndroid (string url,string newPath){
  6.  
  7. using(WWW www = new WWW(url)){
  8.  
  9. yieldreturn www;
  10.  
  11. if (www.error != null)
  12.  
  13. Debug.LogError("WWW download:" + www.error);
  14.  
  15. File.WriteAllBytes(newPath, www.bytes);
  16.  
  17. LoadAllResource();
  18.  
  19. Debug.Log("android db download finished");
  20.  
  21. }
  22.  
  23. }

参考资料:

1、https://gist.github.com/esperlu/943776

2、http://docs.unity3d.com/Documentation/ScriptReference/Application-streamingAssetsPath.html

Unity3d 中 将远程 MySQL 数据库转换为本地 Sqlite的更多相关文章

  1. phpMyAdmin访问远程MySQL数据库的方法

    本地phpmyadmin远程连接服务器端MySQL 首先要确定你的mysql远程连接已开启,如果没有开启按照下面的二个方法操作: 方法一:改表法 因为在linux环境下,默认是关闭3306端口远程连接 ...

  2. Navicat备份远程Oracle数据库到本地

    公司的数据库是本地的,我只能在公司连,回家就不能跑项目了,一跑就报SQLException,所以希望可以把数据库复制到我的本地来. 因为一直在用Navicat操作数据库,这里就分享一下用Navicat ...

  3. MYSQL数据库自动本地/异地双备份/MYSQL增量备份

    构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)架构图 继续介绍Linux服务器文件备份,数据库备份,数据安全存储相关的电子商务系统架构.针对安全性有多种多样的解决方案,其中数据备份是 ...

  4. 连接远程MySQL数据库项目启动时,不报错但是卡住不继续启动的,

    连接远程MySQL数据库项目启动时,不报错但是卡住不继续启动的, 2018-03-12 17:08:52.532DEBUG[localhost-startStop-1]o.s.beans.factor ...

  5. 【MySql 】is not allowed to connect to this MySql server 无法访问远程MySQL数据库

    问题:访问远程MySQL数据库时报错[is not allowed to connect to this MySql server ]不允许访问,因为MySQL默认只有本机localhost能够访问M ...

  6. centos7--web项目使用远程mysql数据库

    07-django项目连接远程mysql数据库   比如电脑a(ip地址为192.168.0.aaa)想要连接访问电脑b(ip地址为192.168.0.bbb)的数据库: 对电脑a(ip地址为192. ...

  7. 知识点:Navicet Mysql数据库电脑本地备份

    Navicet Mysql数据库电脑本地备份 1.打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击“计划”,再点击“新建批处理作业”.     2.双击上面的可用任务,它就 ...

  8. nodejs中如何使用mysql数据库[node-mysql翻译]

    nodejs中如何使用mysql数据库 db-mysql因为node-waf: not found已经不能使用,可以使用mysql代替. 本文主要是[node-mysql]: https://www. ...

  9. Navicet Mysql数据库电脑本地备份

    Navicet Mysql数据库电脑本地备份 1.打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击"计划",再点击"新建批处理作业" ...

随机推荐

  1. bash shell笔记4 处理用…

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/521448 知 ...

  2. linux驱动模块编译(初学者)

    inux 模块编译步骤(转) 本文将直接了当的带你进入linux的模块编译.当然在介绍的过程当中,我也会添加一些必要的注释,以便初学者能够看懂.之所以要写这篇文章,主要是因为从书本上学的话,可能要花更 ...

  3. 解决Maximum execution time of 120 seconds exceeded

    在循环开始前加入代码: //设置超时时间 ini_set("max_execution_time",18000); set_time_limit(0); set_time_limi ...

  4. java基础知识(三)之数组

    声明数组: 语法:数据类型[ ] 数组名://例:int[ ] scores;  或者 数据类型 数组名[ ]://例:int scores[ ];分配空间 语法:数组名 = new 数据类型 [ 数 ...

  5. android 记录所有打开的Activity,退出程序

    android 记录所有打开的Activity,退出程序   package com.main.server; import java.util.LinkedList; import java.uti ...

  6. SpringMVC Controller 介绍(详细深刻)

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  7. msql 计算连续签到天数

    刚刚写了一个签到计算天数的sql, 记录下来. 思路如下: 获取当前签到的最后时间(今天或昨天), 定义一个变量@i 对签到时间进行天数自减, 然后查询出当前记录签到时间是否与自减后的时间匹配.   ...

  8. ssh时传递环境变量

    设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...

  9. Vue.js如何搭建本地dev server和json-server 模拟请求服务器

    前言:vue-cli(版本更新),由原来的2.8.1升级为2.9.1.主要改变是原来在build文件夹下的dev-server.js删掉了,增加了webpack.dev.conf.js. 所以这次讲的 ...

  10. 如果你的资源贫乏,那么专注做好一件事将是你的唯一出路(no reading yet)

    http://www.jianshu.com/p/8784f0fd7ab8/comments/1161511