设为首页 收藏本站
查看: 1315|回复: 0

[经验分享] Unity3d 中 将远程 MySQL 数据库转换为本地 Sqlite

[复制链接]

尚未签到

发表于 2015-6-26 00:32:08 | 显示全部楼层 |阅读模式
  1、创建MySQL2Sqlite脚本mysql2sqlite.sh:(代码地址:https://gist.github.com/esperlu/943776)


DSC0000.gif DSC0001.gif


#!/bin/sh
# Converts a mysqldump file into a Sqlite 3 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 = 1
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 != 0 { print; next }
# Skip other comments
/^\/\*/ { next }
# Print all `INSERT` lines. The single quotes are protected by another single quote.
/INSERT/ {
gsub( /\\\047/, "\047\047" )
gsub(/\\n/, "\n")
gsub(/\\r/, "\r")
gsub(/\\"/, "\"")
gsub(/\\\\/, "\\")
gsub(/\\\032/, "\032")
print
next
}
# Print the `CREATE` line as is and capture the table name.
/^CREATE/ {
print
if ( match( $0, /\"[^\"]+/ ) ) 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(/\([0-9]+\)/, "") }
# 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 = $1
}
# `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 ($0 == ");"){
print
} else {
if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
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 0
View Code   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
View Code
修改相应的参数:
  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);
}
}
View Code  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");
}
}
View Code   
  参考资料:
  1、https://gist.github.com/esperlu/943776
  2、http://docs.unity3d.com/Documentation/ScriptReference/Application-streamingAssetsPath.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-80551-1-1.html 上篇帖子: Entity Framework:第三方开发MySQL,Oracle,SQLite ADO.NET Provider支持Entity Framework 下篇帖子: sqlite,mysql,access对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表