Posts

Showing posts with the label database

Yingshaoxo's database design (version 2)

Initiate First, create a folder called database Then, create a yingshaoxo_database_index.txt file under that database folder. For each line, we’ll put a relative_path towards a json_file that under some sub_folders like ./1/current_index_row_number_plus_random_hash_string.json Add  When you add a record, you first read how many lines the yingshaoxo_database_index.txt has, let’s say, index_length = 11 Then you create a json file that has the new data with a name of 11_random_hash_string.json After that, you append that json path at the bottom of yingshaoxo_database_index.txt file. For windows system, it can’t have more than 4,294,967,295 files under a folder. So you’d better seperate files into different folders. Search When you search for a record, you iterate all those json path inside yingshaoxo_database_index.txt . You read each json object. For each json object, you use a python function to handle it. If a record is not what you want, you return None, if a reco...

Yingshaoxo's database design

Initiate First, create a database.txt file. Then, for each line, it is a json string. That json string is actually a dict. Add When you add a record, you append a new json string at the bottom of that database.txt file. Search When you search for a record, you iterate all those json string. For each line of json string, you use a python function to handle it. If a record is not what you want, you return None, if a record is what you want, you return a dict result. In the end, you’ll ignore those None result, and get a list of dict object. >>> import subprocess >>> pycode = """ ... import sys ... if sys.argv[1] == 'nice_guy': ... print('yingshaoxo') ... else: ... print('unrecognized arg') ... """ >>> result = subprocess.run([ 'python' , '-c' , pycode, 'nice_guy' ], stdout=subprocess.PIPE) >>> print(result.stdout.decode()) yingshaoxo Delete I do not r...

How to recovery mongodb data after a sudden server shutdown in docker under Linux

I suppose you have mounted the mongo /data/db into a local folder with this command:  volumes: - "./configs/mongodb_data:/data/db" Then you should run this command to recovery the data: chmod 777 /root/it_has_alternatives/configs/mongodb_data docker container run --rm -v /root/it_has_alternatives/configs/mongodb_data:/data/db mongo: 6.0 . 4 -jammy mongod --repair If your docker has some serious problem, you could run the following command to repair it: Make sure all docker containers are down: docker compose down Remove problematic overlay2: sudo rm -R /var/lib/docker/overlay2 Remove images: sudo rm -R /var/lib/docker/image Clear any other cached data: sudo docker system prune -f Restart docker service: sudo systemctl restart docker ​

Understand the query command that uses two tables in MySQL(or Postgresql)

Image
mysql join create table student(id integer, name varchar(100)); insert into student(id, name) values(0, "yingshaoxo"); insert into student(id, name) values(1, "god"); create table takes(id integer, course varchar(100)); insert into takes(id, course) values(0, "english"); insert into takes(id, course) values(0, "coding"); insert into takes(id, course) values(0, "business"); insert into takes(id, course) values(1, "english"); insert into takes(id, course) values(2, "web_design"); select * from student; select * from takes; select student.id, student.name, takes.course from (student join takes on student.id = takes.id); Other join keywords that may not useful SELECT whatever FROM TableA a INNER JOIN TableB b ON a.column_name_1 = b.column_name_1 WHERE b.ok = 'yes' SELECT order_table.id AS Id, user_table.banned AS User_got_banned, order_table.request_at AS Day , order_table. status AS Order_st...

How to export and import postgresql data

If you need to get into a docker container: docker exec -it ddc7665629f0 sh Export  pg_dump -U username database_name > ./test.pgsql Import psql -U username database_name < ./test.pgsql ​

How to force django database migrations back to normal

1. delete all migration files   find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete 2. drop that database at your SQL shell mysql -u root -p show databases; DROP database your_database_name; 3. create new initial migrations and generate the database tables python manage.py makemigrations python manage.py migrate https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html ​

How to let mysql database support chinese or utf-8 charactres

```bash DROP database your_database_name create database your_database_name default character set utf8mb4 collate utf8mb4_unicode_ci; ```