代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

SQL数据备份 SQL

创建数据库备份

SELECT * INTO OUTFILE '/path/to/backup.csv'
  FIELDS TERMINATED BY ','
  OPTIONALLY ENCLOSED BY '"'
FROM users;
JS Chart.js JavaScript

绘制柱状图

const ctx = document.getElementById('myChart').getContext('2d');
const data = {
  labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
  datasets: [{
    label: 'Sales',
    data: [65, 59, 80, 81, 56],
    backgroundColor: 'rgba(255, 99, 132, 0.2)',
    borderColor: 'rgba(255, 99, 132, 1)',
    borderWidth: 1
  }]
};

const config = {
  type: 'bar',
  data: data,
  options: {
    responsive: true,
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
};

new Chart(ctx, config);
PHP GD库 PHP

创建缩略图

🔥             
Python爬虫 Python

BeautifulSoup解析

import requests
from bs4 import BeautifulSoup

response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')

title = soup.title.text
print(f'Title: {title}')

for link in soup.find_all('a'):
    print(link.get('href', None))
Java邮件 Java

发送SMTP邮件

import javax.mail.*;
import javax.mail.inet.*;

public class EmailSender {
    public static void sendEmail(String to, String from, String subject, String body) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.example.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthenticator getPasswordAuthenticator() {
                return new PasswordAuthenticator("username", "password");
            }
        });
        
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject);
        message.setText(body);
        
        Transport.send(message);
    }
}
C++并行排序 C++

使用OpenMP并行排序

#include <omp.h>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<int> data(10000000);
    // Fill data with random values
    for (auto& n : data) n = rand() % 1000;
    
    #prag omp parallel
    #prag omp for
    sort(data.begin(), data.end());
    
    cout << "Sorted " << data.size() << " elements\n";
    return 0;
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类