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

#!/bin/sh

# Converts a mysqldump file into a Sqlite  compatible file. It also extracts the MySQL `KEY xxxxx` from the
# CREATE block and create them in separate commands _after_ all the INSERTs. # Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
# The mysqldump file is traversed only once. # Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
# Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite # Thanks to and @artemyk and @gkuenning for their nice tweaks. mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \ awk ' BEGIN {
FS=",$"
print "PRAGMA synchronous = OFF;"
print "PRAGMA journal_mode = MEMORY;"
print "BEGIN TRANSACTION;"
} # CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
/^\/\*.*CREATE.*TRIGGER/ {
gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
print
inTrigger =
next
} # The end of CREATE TRIGGER has a stray comment terminator
/END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next } # The rest of triggers just get passed through
inTrigger != { print; next } # Skip other comments
/^\/\*/ { next } # Print all `INSERT` lines. The single quotes are protected by another single quote.
/INSERT/ {
gsub( /\\\/, "\047\047" )
gsub(/\\n/, "\n")
gsub(/\\r/, "\r")
gsub(/\\"/, "\"")
gsub(/\\\\/, "\\")
gsub(/\\\/, "\032")
print
next
} # Print the `CREATE` line as is and capture the table name.
/^CREATE/ {
print
if ( match( $, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
} # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
/^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) } # Get rid of field lengths in KEY lines
/ KEY/ { gsub(/\([-]+\)/, "") } # Print all fields definition lines except the `KEY` lines.
/^ / && !/^( KEY|\);)/ {
gsub( /AUTO_INCREMENT|auto_increment/, "" )
gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
gsub( /(COLLATE|collate) [^ ]+ /, "" )
gsub(/(ENUM|enum)[^)]+\)/, "text ")
gsub(/(SET|set)\([^)]+\)/, "text ")
gsub(/UNSIGNED|unsigned/, "")
if (prev) print prev ","
prev = $
} # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
# in a separate `CREATE KEY` command. The index name is prefixed by the table name to
# avoid a sqlite error for duplicate index name.
/^( KEY|\);)/ {
if (prev) print prev
prev=""
if ($ == ");"){
print
} else {
if ( match( $, /\"[^"]+/ ) ) indexName = substr( $, RSTART+, RLENGTH- )
if ( match( $, /\([^()]+/ ) ) indexKey = substr( $, RSTART+, RLENGTH- )
key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
}
} # Print all `KEY` creation lines.
END {
for (table in key) printf key[table]
print "END TRANSACTION;"
}
'
exit

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

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

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

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

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

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Diagnostics; public class ChangeDB {
static string sqliteDBpath = Application.streamingAssetsPath + "/dbFile/";
static string sqliteDBname = "sqlite_db_name.db";
[MenuItem("Tools/Mysql to Sqlite")]
public static void mysqlToSqlite ()
{
if(File.Exists(sqliteDBpath)){
UnityEngine.Debug.Log("delete old sqlite db file...");
File.Delete(sqliteDBpath+sqliteDBname);
}
if(File.Exists(sqliteDBpath+sqliteDBname+".meta")){
File.Delete(sqliteDBpath+sqliteDBname+".meta");
}
//start to convert mysql to sql
UnityEngine.Debug.Log("start convert...");
ProcessStartInfo proc = new ProcessStartInfo();
proc.WorkingDirectory = sqliteDBpath;
proc.FileName = "sh";
proc.Arguments = "convertDB.sh";
proc.UseShellExecute = false;
proc.Verb = "";
Process.Start(proc);
}
}

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

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

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

string sqlitedbPath = Application.streamingAssetsPath + "/dbFile/dbname.db";

string filepath = Application.persistentDataPath + "/dbname.db";

 IEnumerator LoadSqliteDBForAndroid (string url,string newPath){

using(WWW www = new WWW(url)){

yieldreturn www;

if (www.error != null)

Debug.LogError("WWW download:" + www.error);

File.WriteAllBytes(newPath, www.bytes);

LoadAllResource();

Debug.Log("android db download finished");

        }

   }

参考资料:

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. HTML中 .clearfix:after的使用

    将一个<div>标签随着内容的增加而增加

  2. eclipse egit(远程仓库)

    Git的强大之一体现在远程仓库,Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.怎么分布呢?最早,肯定只有一台机器有一个原始版本库,此后,别的机器可以“克隆”这个原始版本库,而且 ...

  3. 自定义的parse_url逆向函数http_build_url,将数组转为url字符串

    parse_url函数可以讲一个url字符串解析成一个数组,但是php中似乎没有parse_url的逆向函数来讲一个解析出来的数组组合成url字符串,只有一个http_build_query用来将数组 ...

  4. 744. Find Smallest Letter Greater Than Target 查找比目标字母大的最小字母

    [抄题]: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...

  5. LaTeX入门教程(一)

    LaTeX(LATEX,音译“拉泰赫”)是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计 ...

  6. asp.net服务器推送长连接

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  7. rpmbuild spec 打包jar变小了、设置禁止压缩二进制文件Disable Binary stripping in rpmbuild

    Disable Binary stripping in rpmbuild 摘自:http://livecipher.blogspot.com/2012/06/disable-binary-stripp ...

  8. Laravel Gate 授权方式的使用指南

    参考链接:An Introduction to Laravel Authorization Gates 本文使用 Laravel 的 Gate 授权方式 实现一个基于用户角色的博客发布系统. 在系统包 ...

  9. [GO]将随机生成的四位数字拆分后放到一个切片里

    package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) ...

  10. Map存储容量及内存占用测试

    Integer a = 1; long start = 0; long end = 0; // 先垃圾回收 System.gc(); start = Runtime.getRuntime().free ...