搜索引擎--范例:谈谈django--mysql数据库的一些常用命令

现在基本没有什么能离得开数据库了,django我一直用的都是mysql的数据库,这次和大家说说django--mysql数据库的一些常用命令吧

1:命令行登陆mysql

C:\Users\Administrator>mysql -uroot -p
Enter password: ********

2:创建一个新的数据库并使用它

mysql> create database t;
Query OK, 1 row affected (0.02 sec)

mysql> use t;
Database changed

3:说点注意问题,我们先练show一下

mysql> show create database t;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| t        | CREATE DATABASE `t` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

注意到字符集是Latin1的

这样就无法存中文了,下面我们改一下设置吧,具体的命令我就不一一解说了,相信大家都能看得懂

mysql> show variables like char%;
+--------------------------+----------------------------------------------------
-----+
| Variable_name            | Value
     |
+--------------------------+----------------------------------------------------
-----+
| character_set_client     | latin1
     |
| character_set_connection | latin1
     |
| character_set_database   | latin1
     |
| character_set_filesystem | binary
     |
| character_set_results    | latin1
     |
| character_set_server     | latin1
     |
| character_set_system     | utf8
     |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.5\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.01 sec)

mysql> set names utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like char%;
+--------------------------+----------------------------------------------------
-----+
| Variable_name            | Value
     |
+--------------------------+----------------------------------------------------
-----+
| character_set_client     | utf8
     |
| character_set_connection | utf8
     |
| character_set_database   | latin1
     |
| character_set_filesystem | binary
     |
| character_set_results    | utf8
     |
| character_set_server     | latin1
     |
| character_set_system     | utf8
     |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.5\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)


mysql> set character_set_database = utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_server = utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like char%;
+--------------------------+----------------------------------------------------
-----+
| Variable_name            | Value
     |
+--------------------------+----------------------------------------------------
-----+
| character_set_client     | utf8
     |
| character_set_connection | utf8
     |
| character_set_database   | utf8
     |
| character_set_filesystem | binary
     |
| character_set_results    | utf8
     |
| character_set_server     | utf8
     |
| character_set_system     | utf8
     |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.5\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)

3:然后把1,2步骤中创建的数据库删掉,drop database +数据库名就可以了,然后重新创建就ok了

4:现在配置django中的配置文件

DATABASES = {
    default: {
        ENGINE: django.db.backends.mysql, # Add ‘postgresql_psycopg2‘, ‘mysql‘, ‘sqlite3‘ or ‘oracle‘.
        NAME: irsearch,                      # Or path to database file if using sqlite3.
        USER: root,                      # Not used with sqlite3.
        PASSWORD: ‘你的数据库的密码,                  # Not used with sqlite3.
        HOST: 127.0.0.1,                      # Set to empty string for localhost. Not used with sqlite3.
        PORT: ‘‘,                      # Set to empty string for default. Not used with sqlite3.
    }
}

5:写model.py了,简单举例,注意一定要用models.Model,否则没用的

#-*- coding: utf-8 -*-
from django.db import models

# Create your models here.
class Weibo(models.Model):
    """docstring for weibo"""
    w_id = models.IntegerField(primary_key=True)
    userid = models.CharField(max_length=20)
    username = models.CharField(max_length=30)
    content = models.CharField(max_length=2000)
    ptime = models.CharField(max_length=20)
    count = models.IntegerField()

6:django的SQL操作

python manage.py syncdb 初始化数据库或者更新数据库

python manage.py sql +数据库名字

python manage.py  sql +数据库名字 > t.sql 可以让这些数据写入到t.sql中,可以直接用

6:mysql的数据导入导出

其实强烈不建议使用mysql的数据导入导出的,可以通过后台直接读取数据库而进行数据库中的数据的导入导出

C:\Users\Administrator>mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
                                           数据库名   表名
C:\Users\Administrator>mysqldump -uroot -p irsearch search_stop >stop.txt
Enter password: ********

然后就可以在当前目录下看到stop.txt了,最好是输出到sql文件里面,然后就可以直接导入sql了

如果是导入新浪SAE的数据库,记得吧里面的LOCK,UNLOCK 指令去掉,因为SAE没有给你这个指令的权限

 

最后一定提醒大家,注意数据库的存放格式,否则你会哭的,相信我

搜索引擎--范例:谈谈django--mysql数据库的一些常用命令,布布扣,bubuko.com

搜索引擎--范例:谈谈django--mysql数据库的一些常用命令

上一篇:[BAT]通过schtasks.exe远程调用windows 2008 server上的计划任务,提示ERROR : Access is denied


下一篇:SQL触发器实例讲解