创建视图指定字符编码
时间: 2023-11-03 13:05:36 浏览: 36
可以在创建视图时指定字符编码,例如:
```
CREATE VIEW my_view
AS SELECT *
FROM my_table
WHERE condition
WITH CHECK OPTION
CHARACTER SET utf8mb4;
```
在上面的语句中,`CHARACTER SET`指定了视图的字符编码为`utf8mb4`。当然,也可以在创建表时指定字符编码,这样创建的视图会继承表的字符编码。例如:
```
CREATE TABLE my_table (
...
) CHARACTER SET utf8mb4;
CREATE VIEW my_view
AS SELECT *
FROM my_table
WHERE condition
WITH CHECK OPTION;
```
在上面的语句中,`my_table`表的字符编码为`utf8mb4`,因此`my_view`视图的字符编码也会是`utf8mb4`。
相关问题
spring配置文件的thymeleaf视图解析器的代买
在 MVC的配置文件中,可以通过以下代码配置Thymeleaf视图解析器:
```
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>
```
上述代码中,`templateResolver`用于配置Thymeleaf模板的位置、后缀和模板的类型,`templateEngine`用于创建Thymeleaf模板引擎,`ThymeleafViewResolver`用于将Thymeleaf模板渲染成HTML视图。
具体解释如下:
1. `templateResolver`配置:
- `SpringResourceTemplateResolver`是Thymeleaf提供的一个模板解析器,它可以从classpath或文件系统中加载模板文件。
- `prefix`属性用于指定模板文件所在的目录。
- `suffix`属性用于指定模板文件的后缀。
- `templateMode`属性用于指定模板的类型,HTML5为默认类型。
2. `templateEngine`配置:
- `SpringTemplateEngine`是Thymeleaf提供的一个模板引擎,它可以根据`templateResolver`配置创建一个模板解析器。
- `templateResolver`属性用于指定模板解析器。
3. `ThymeleafViewResolver`配置:
- `ThymeleafViewResolver`用于将Thymeleaf模板渲染成HTML视图。
- `templateEngine`属性用于指定模板引擎。
- `characterEncoding`属性用于指定字符编码。
配置完成后,在Controller中返回的逻辑视图名将会被解析为Thymeleaf模板文件的名称。例如,如果逻辑视图名为`"home"`,则对应的Thymeleaf模板文件为`/WEB-INF/templates/home.html`。
需要注意的是,Thymeleaf视图解析器的版本可能会随着Spring框架的版本而有所变化,上述代码中的版本号为5.x,如果您使用的是其他版本的Thymeleaf或Spring框架,请根据实际情况进行调整。
def get(request): # 实例化SSHClient连接工具 ssh_client = paramiko.SSHClient() # 保存服务器密钥 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接linux服务器 try: ssh_client.connect(hostname='192.168.233.128', port=22, username='root', password='12356789', timeout=2) except (EOFError, Exception) as e: request_data = {"code": 500, "message": "服务器连接失败", "data": []} return JsonResponse(request_data) # 返回了三个数据,第一个是输入命令,第2个是命今返回的结果,第3个是命令错误时返回的结果 # ps aux是linux服务器查询正在运行的所有应用程序的命令 stdin, stdout, stderr = ssh_client.exec_command('ps aux') # 读取返回结果,将编码转为utf8,避免乱码 df = stdout.read().decode('utf-8') # 创建一个临时文件存放命令返回的结果 # 因为linux服务器命令返回的数据是一个字符串,不能直接按空格截取,最好的方式就是写入文件读取一行再截取、拼接命令 # IO流, 数据存储到本地磁盘 filename = "tempdata.txt" # 打开文件,会在项目根目录打开,如果没有就会创建 w+为写入读取权限 fileObj = open(filename, mode='w+') # 将命令返回的结果写入文件 fileObj.write(df) # flush:保证数据成功写入文件。 fileObj.flush() fileObj.close()
这段代码是一个Django视图函数,实现了连接到一个Linux服务器并执行命令的功能。具体实现步骤如下:
1. 首先导入paramiko模块,该模块提供了SSH客户端的实现,可以用来连接远程Linux服务器。
2. 创建一个SSHClient对象,用来连接到Linux服务器,并设置自动添加新主机密钥的策略。
3. 使用connect()方法连接到指定的Linux服务器,需要传入服务器的IP地址、端口、用户名和密码等信息。
4. 如果连接失败,返回一个Json格式的错误信息。
5. 使用exec_command()方法执行命令,该方法返回三个对象:标准输入、标准输出和标准错误。在这里只需要标准输出对象。
6. 使用read()方法读取标准输出对象的内容,并将编码转换为UTF-8格式。
7. 创建一个临时文件,将命令返回的结果写入该文件。
8. 关闭文件并返回HttpResponse对象,该对象包含了命令返回的结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)