解析连接到python脚本的MySQL数据库

时间:2015-08-30 12:28:19

标签: python mysql mysql-python

我使用python-mysqldb包创建了这个python脚本,我试图从Bible.sql文件中打印一个请求的经文。 (警告:链接的32文件很大,我已经对下面的sql语句进行了采样)

我对查询SQL数据库并不完全熟悉,到目前为止我能做的只有.sqlshow databases; show tables;

select * from bible;

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | openVdev | +--------------------+ 2 rows in set (0.00 sec) mysql> show tables; +--------------------+ | Tables_in_openVdev | +--------------------+ | bible | +--------------------+ 1 row in set (0.00 sec) 只为我转储一切,我如何解析文件以获得某段经文/圣经?

以下是SQL语法的示例:

select * from bible;

还有几个版本的圣经由关键值确定:

INSERT INTO `bible` VALUES (1,'Genesis',1,1,'IN THE beginning God 
INSERT INTO `bible` VALUES (531,'Genesis',21,17,'And God heard the voice
INSERT INTO `bible` VALUES (1035,'Genesis',35,23,'The sons of Leah: Reuben,
INSERT INTO `bible` VALUES (1531,'Genesis',50,24,'And Joseph said to his brethren,
INSERT INTO `bible` VALUES (1985,'Exodus',17,1,'ALL THE congregation

我按照post创建了python-MySQLdb连接

DROP TABLE IF EXISTS `bible`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bible` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `book` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `chapter` int(11) NOT NULL,
  `verse` int(11) NOT NULL,
  `AMP` text COLLATE utf8_unicode_ci NOT NULL,
  `ASV` text COLLATE utf8_unicode_ci NOT NULL,
  `BENG` text COLLATE utf8_unicode_ci NOT NULL,
  `CEV` text COLLATE utf8_unicode_ci NOT NULL,
  `DARBY` text COLLATE utf8_unicode_ci NOT NULL,
  `ESV` text COLLATE utf8_unicode_ci NOT NULL,
  `KJV` text COLLATE utf8_unicode_ci NOT NULL,
  `MKJV` text COLLATE utf8_unicode_ci NOT NULL,
  `MSG` text COLLATE utf8_unicode_ci NOT NULL,
  `NASB` text COLLATE utf8_unicode_ci NOT NULL,
  `NIV` text COLLATE utf8_unicode_ci NOT NULL,
  `NKJV` text COLLATE utf8_unicode_ci NOT NULL,
  `NLT` text COLLATE utf8_unicode_ci NOT NULL,
  `NRSV` text COLLATE utf8_unicode_ci NOT NULL,
  `WEB` text COLLATE utf8_unicode_ci NOT NULL,
  `YLT` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`book`,`chapter`,`verse`),
  KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=31103 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

如果看起来我没有做出任何努力,我道歉,但这是一个没有进展的月份。我很难搞清楚如何使用命令行查询MySQL数据库,尽管我已经能够用tutorial取得一些进展:

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="openVuser", # your username
                      passwd="openVpw", # your password
                      db="openVdev") # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor() 

# Use all the SQL you like
cur.execute("SELECT * FROM bible")

Question_1 = "\n Please enter a scripture?\n"
scripture = raw_input(Question_1)

print scripture


print all the first cell of all the rows
for row in cur.fetchall() :
    print row[0]    

我对圣经表中的KJV键感兴趣,我怎么能选择那个键并转到经文,并将用户收到的输入解析为SQL查询?

mysql> describe bible;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | MUL | NULL    | auto_increment |
| book    | varchar(25) | NO   | PRI | NULL    |                |
| chapter | int(11)     | NO   | PRI | NULL    |                |
| verse   | int(11)     | NO   | PRI | NULL    |                |
| AMP     | text        | NO   |     | NULL    |                |
| ASV     | text        | NO   |     | NULL    |                |
| BENG    | text        | NO   |     | NULL    |                |
| CEV     | text        | NO   |     | NULL    |                |
| DARBY   | text        | NO   |     | NULL    |                |
| ESV     | text        | NO   |     | NULL    |                |
| KJV     | text        | NO   |     | NULL    |                |
| MKJV    | text        | NO   |     | NULL    |                |
| MSG     | text        | NO   |     | NULL    |                |
| NASB    | text        | NO   |     | NULL    |                |
| NIV     | text        | NO   |     | NULL    |                |
| NKJV    | text        | NO   |     | NULL    |                |
| NLT     | text        | NO   |     | NULL    |                |
| NRSV    | text        | NO   |     | NULL    |                |
| WEB     | text        | NO   |     | NULL    |                |
| YLT     | text        | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
20 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:2)

/* Generic query */
SELECT `key1`, `key2`, `key3` FROM `table` WHERE `another_key` LIKE 'SOME_STRING';

/* Specific query for your problem */
SELECT `KJV` FROM `bible` WHERE `chapter` LIKE 'Romans' AND `verse` = '12';