How do I use a database in a custom theme?

Alright, You did not mention if that data will be stored upon submission from front end or you want to manually put that in. But lets see the way I would handle form submissions and store email addresses. I would use contact form 7 along with its addon Flamingo. It will let you store all the email submissions made through the contact form. I hope that is the flow you are looking for.

Also, to insert something into wordpress database, you need to write a simple query, such as:

Put This Inside your functions.php

global $wpdb; 
$insert_into_database = $wpdb->query($wpdb->prepare('INSERT INTO your_table_name (column1,column2) VALUES (%s,%d)',array($value_1,$value_2)));
if($insert_into_database){
  // Do something on successfully inserting data.
}

Also we are using $wpdb->prepare to prepare our sql for safe execution.

%s – string (value is escaped and wrapped in quotes)
%d – integer
%f – float
%% – % sign

I hope this solves your problem! Cheers 🙂