北大侠客行MUD论坛

 找回密码
 注册
搜索
热搜: 新手 wiki 升级
查看: 8812|回复: 7

zmud721中操作数据库

[复制链接]
发表于 2010-9-27 09:19:24 | 显示全部楼层 |阅读模式
在zmud721中操作数据库是不是必须在DB窗口中把要操作的数据库打开?
如果我仅仅在命令行中使用#DBLOAD **打开数据库而没有在DB窗口中打开的话,
实际上我是操作不了的。这是Zmud的一个bug还是我配置的不对?
谢谢。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-9-27 11:02:57 | 显示全部楼层
不需要,你不要用系统的数据库,那个东西我没搞明白,你自己打开access数据库就行。zmud支持打开access数据库。但是怎么打开别的数据库,网上没有连接字符串的配置例子,不会配置。
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-9-27 14:57:11 | 显示全部楼层

回复 1楼 的帖子

先用db那个按钮打开数据库,再#DBLOAD
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-10-8 11:12:54 | 显示全部楼层
原帖由 seagate 于 2010-9-27 11:02 AM 发表
不需要,你不要用系统的数据库,那个东西我没搞明白,你自己打开access数据库就行。zmud支持打开access数据库。但是怎么打开别的数据库,网上没有连接字符串的配置例子,不会配置。


如何读写access数据库啊,有代码讲解么?
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-10-8 14:14:22 | 显示全部楼层
访问access需要com,可以参考这里:
http://www.zuggsoft.com/page.php?file=zmud/comado.htm
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-10-8 14:39:31 | 显示全部楼层
原帖由 killunix 于 2010-10-8 02:14 PM 发表
访问access需要com,可以参考这里:
http://www.zuggsoft.com/page.php?file=zmud/comado.htm


公司上不了外网,上次在seagate某个机器人里面看到过...没有仔细看
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2010-10-8 14:49:23 | 显示全部楼层
  1. ADO Programming in zMUD


  2. Updated: by Mike Potter (Zugg)

  3. What is ADO?
  4. NOTE: This document is written for zMUD v6.26 and later.

  5. ADO stands for ActiveX Data Objects. It is a collection of routines used to access databases independent of the actual database format.

  6. ADO was added to the Windows operating system in Windows 98. It comes with Windows 98, ME, 2000, and XP. It is also added to your system if you install Internet Explorer 5.0 (or later), or Microsoft Office.

  7. Windows 95 and Windows NT systems that do not have ADO can easily download and install it for free. Go to the zMUD Database page for details on doing this.

  8. Starting with version 6.26, zMUD uses ADO for all of it's database chores. Since zMUD databases use ADO, these database can be accessed using any language that can use ADO, which includes most every language available, including Visual Basic, and even ASP web pages! By using the ADO database engine already built into Windows, zMUD no longer has to add it's own proprietary database engine, which reduces the size of zMUD (although starting zMUD is slightly slower since it has to load ADO from Windows).

  9. NOTE: zMUD doesn't yet use ADO for it's Database Module. Only the MUD List and Character List are in ADO format. Future versions of zMUD will include ADO format for the Mapper and the Database modules.

  10. Since you can access ADO databases from any programming language that supports ADO, and ADO is simply a COM interface, that means you can access ADO from within zMUD scripts. For more details on calling COM objects in zMUD, see the COM Programming in zMUD article. This article will show some examples of using ADO within zMUD.

  11. Connecting to a database
  12. Before you can read data from a database, you must connect to it. First, you create the ADO Connection COM object:

  13. #VAR Conn %comcreate( "ADODB.Connection")
  14. Next, you must open a specific database. This is the most complex part of ADO. In order to connect to a database, you must specify the database driver used to access the data. For example, if the database in an SQL Server database, you specify SQL Server. If it's a Oracle database, you specify Oracle. If the database is already defined as a system-wide DSN on your computer, you can open it with just the DSN name:

  15. #CALL @Conn.Open( "DSNName")
  16. To create a DSN, go to the ODBC Control Panel and use it to add a new DSN to your system. The details of this are beyond the scope of this document.

  17. For zMUD databases, it's a bit easier. All zMUD Databases are in Microsoft Access 2000 format. The database driver for Access 2000 is Microsoft.Jet.OLEDB.4.0. So, to open a specific Microsoft Access 2000 database, you use the following command:

  18. #CALL @Conn.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filename.mdb")
  19. where "filename.mdb" is the name of the Microsoft Access 2000 database file. As an example, let's open the MUD list database used by zMUD, stored in the file mudlist.mdb:

  20. #CALL @Conn.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mudlist.mdb")
  21. To verify that a database is open, you can display the full ConnectionString property of the database:

  22. #ECHO @Conn.ConnectionString
  23. If this property has a long value, then the database is open and ready to go.

  24. Querying the database
  25. To search the database for the records that you are interested in, you must query the database using SQL. SQL is a language designed for database queries. These days, most every database supports SQL. So, get yourself a good book on SQL since it is way beyond the scope of this article.

  26. One tricky part about SQL is that some details can be specific to the exact type of database you are working with. Some SQL commands that work with SQL Server or Oracle, will not work with a Microsoft Access 2000 database. But for typical applications you don't have to worry about this.

  27. The most common way to query a database is using the SQL SELECT statement. It has the syntax:

  28. SELECT Fields FROM TableName WHERE Field = 'Value'
  29. To retrieve all of the Fields, use * in place of Fields. For the MUD List, there is only one table in the database, and it is called MUDLIST. One of the fields in the table is called "Title" which is the name of the MUD. So, to query the database for the MUD record for "Realms of Despair", the SQL command would be:

  30. SELECT * FROM Mudlist WHERE Title = 'Realms of Despair'
  31. Now that we have the SQL statement, we need to execute this on our open connection. The result of the execution is a object called a RecordSet which is a set of all records in the database that match our query. So, the zMUD command for this would be:

  32. #VAR rs @Conn.Execute( "SELECT * FROM Mudlist WHERE Title = 'Realms of Despair'")
  33. Be sure and get the quotes correct. SQL wants a single quote around the field value, and zMUD wants double quotes around the entire SQL statement.

  34. To display the entire contents of this record set as a string, use the GetString property:

  35. #ECHO @rs.GetString
  36. There's the data!

  37. Using RecordSets instead of Connection objects
  38. The method described above works well, but is limited in several ways. The most important limitation is that the record set returned by the @Conn.Execute method is a read-only record set. To open a recordset that you can modify, you must work with recordset objects directly.

  39. Let's open the same database using only a recordset:

  40. #VAR rs %comcreate( "ADODB.Recordset")
  41. #VAR ConnStr "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mudlist.mdb"
  42. #VAR Sql "SELECT * FROM Mudlist WHERE Title = 'Realms of Despair'"
  43. #CALL @rs.Open( @Sql, @ConnStr)
  44. Notice that you still need the same Connection String to specify the location of the database, and the same SQL statement to perform the query. The Open method of the recordset opens the connection and executes the SQL statement.

  45. The above example still produces a read-only recordset. To change this, we add a couple of more optional parameters to the Open method.

  46. But first, before we can open a new dataset, we must close the existing one:

  47. #CALL @rs.Close
  48. Now we will open a new recordset in read/write mode:

  49. #CALL @rs.Open( @Sql, @ConnStr, 2, 2)
  50. The two additional arguments are for the Cursortype and the Locktype. A value of 2 for the CursorType gives us a dynamic cursor that can move both fowards and backwards, useful for navigating the dataset. The LockType of 2 indicates optimistic record locking for a read/write recordset. For more information on these parameters, consult an ADO reference. You should be careful with these parameters if you are accessing a remote database. Some CursorType and LockType values might cause it to read the entire database across your network connection.

  51. Accessing the data in a record
  52. Once you have a recordset using either of the above methods, you can now start looking at the actual data.

  53. If you don't know the names of the fields in the database, you can find out. The record set has a property called Fields that contains both the data in the record, and the information about the fields.

  54. #VAR Fields @rs.Fields
  55. Let's loop through this object and display all of the field names:

  56. #VAR Count @Fields.Count
  57. #LOOP 0,@Count-1 {#ECHO @Fields(%i).Name}
  58. even better, let's display both the field name and the value:

  59. #LOOP 0,@Count-1 {#ECHO @Fields(%i).Name=@Fields(%i).Value}
  60. To retrieve a specific field, once you know the name, you can simple do this:

  61. #ECHO @Fields("Host").Value
  62. for example, to display the host name for the Realms of Despair MUD.

  63. Default Properties
  64. Some COM objects have a default property. For example, the default property of the recordset is the Fields property. And the default value of the Fields collection is the Value property. So, to display the value of the "Host" field, you can also use the syntax:

  65. #ECHO @rs("Host")
  66. this is the same as

  67. #ECHO @rs.Fields("Host").Value
  68. Note that you can only use default properties when reading a value. To set a value, you must explicitly list the final property. So, to change the value of this field, you would do:

  69. #VAR rs("Host").Value "New Name"
  70. Notice that the "Fields" default can be omitted, but the "Value" default is still required. Also, keep in mind that this just changed the value of the Host field in memory and didn't effect the actual database. To change the actual database contents you need to use the @rs.Update method as shown in the next section.

  71. Changing database values
  72. Once you have a field or recordset variable, it is easy to modify the value of a field. For example, to change the Comments field for the Realms of Despair entry, you could do:

  73. #VAR rs("Comments").Value "Test comment"
  74. #CALL @rs.Update
  75. Note that you must have a read/write recordset for this to work. The call to the Update method writes any changes to the current record back to the database.

  76. Looping through a recordset
  77. Let's open a new recordset that has the entire Character database. This database contains the login information for each of your MUD characters as displayed in the initial Character Selection window.

  78. First, we get a ReadOnly recordset with a Dynamic cursor that points to the character database:

  79. #VAR CharRS %comcreate( "ADODB.Recordset")
  80. #VAR ConnStr "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=chardb.mdb"
  81. #CALL @CharRS.Open( "chardb", @ConnStr, 2)
  82. Notice that instead of an SQL query statement, we just used the name of the entire table in the database called "chardb". Now, let's loop through each record in this record set and display the title of the character to the screen:

  83. #CALL @CharRS.MoveFirst
  84. #WHILE (not @CharRS.eof) {
  85. #ECHO @CharRS("IconCaption")
  86. #CALL @CharRS.MoveNext
  87. }
  88. The MoveFirst method sets the recordset to the first record, and the MoveNext method sets the recordset to the next record.

  89. Closing the database
  90. When you are done with a database, be sure to close your recordset and release the COM objects by assigning blank values to them:

  91. #CALL @rs.Close
  92. #VAR Fields ""
  93. #VAR rs ""
  94. #VAR Conn ""
  95. Setting the Connection variable to null will close the connection to the database. When closing a recordset, you will get an error message if there are pending changes, in which case you need to call the Update method or the Cancel method to save or throw away the changes.

  96. Summary
  97. This article has shown some simple examples of using ADO from within zMUD. This is a complex subject since it requires knowledge of COM Programming, ADO, and the actual database that you want to query. But once you get the hang of it, you'll have some incredible power available for your scripts.
复制代码
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
发表于 2013-1-14 12:37:03 | 显示全部楼层
学习Ing,这个代码可以解决打开及更新,如果想新增数据该怎么写呢?
北大侠客行Mud(pkuxkx.com),最好的中文Mud游戏!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|北大侠客行MUD ( 京ICP备16065414号-1 )

GMT+8, 2024-11-28 12:46 PM , Processed in 0.011351 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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