Advertisement
FastJava

MongoDB & SQL Database for IP hosting

Apr 27th, 2024
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | Software | 0 0
  1. Copyright 2024
  2.  
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4.  
  5. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6.  
  7. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  8.  
  9. from pymongo import MongoClient
  10. import mysql.connector
  11.  
  12. ...
  13.  
  14. # MongoDB Connection
  15. client = MongoClient('customdomain.com', 27017)
  16. db = client['mydatabase']
  17. collection = db['mycollection']
  18.  
  19. # SQL Database Connection
  20. sql_connection = mysql.connector.connect(
  21.     host="your_sql_host",
  22.     user="your_sql_user",
  23.     password="your_sql_password",
  24.     database="your_sql_database"
  25. )
  26.  
  27. # Create a cursor object
  28. sql_cursor = sql_connection.cursor()
  29.  
  30. # Execute SQL query to fetch inserted_ids
  31. sql_cursor.execute("SELECT mongo_id FROM mongo_inserted_ids")
  32.  
  33. # Fetch all the inserted_ids
  34. inserted_ids = [row[0] for row in sql_cursor.fetchall()]
  35.  
  36. ...
  37.  
  38. # Close the SQL cursor and connection
  39. sql_cursor.close()
  40. sql_connection.close()
  41.  
  42. # Print the retrieved inserted_ids
  43. print(inserted_ids)
  44.  
  45. # Define the documents to insert
  46. documents = [
  47.     {"name": "John", "age": 30, "city": "New York"},
  48.     {"name": "Jane", "age": 25, "city": "Chicago"},
  49.     {"name": "Mike", "age": 35, "city": "Los Angeles"}
  50. ]
  51.  
  52. ...
  53.  
  54. # Insert the documents
  55. result = collection.insert_many(documents)
  56.  
  57. # Print the inserted IDs
  58. inserted_ids = result.inserted_ids
  59. print(inserted_ids)
  60.  
  61. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement