Choose a book you like: It must be one you can get the whole text on the compute
ID: 3754307 • Letter: C
Question
Choose a book you like:
It must be one you can get the whole text on the computer
Using it, build a table with one line per sentence, plus an id number.
Write the source code to build this table. Submit the sql file called build.sql and the book.txt
How would I write this in SQLite?
To give a brief background we have been using the Command Prompt, within Windows, to answer our SQLite questions. The assignments before this one were simple getting familiar with SQLite (Making a new database, making 5 tables with 3 rows).
Explanation / Answer
First we create Database for Book
Open command prompt find Drive and type directry
c:SQLite3 build.db
sqlite > create table Book (
a int ,
b text
) ;
sqlite > .schema ( to show table)
sqlite > insertRecordToDB (follow the bellow code for record to table using java )
Firstly, you have to create the database and the table that stores the data from the text file like below,
public class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "mydatabase.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE DB_TABLE(_id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("DatabaseHelper", "Upgrading database, which will destroy all old data");
onCreate(db);
}
}
Then you have to read the text file from the SD card and insert the records to the table created above,
//Define two variables,
DatabaseHelper databaseHelper;
SQLiteDatabase db;
// Initiate them in the onCreate method,
databaseHelper = new DatabaseHelper(context);
db = databaseHelper.getWritableDatabase();
//Define methods to save the data to database
void saveDataToDB()
{
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
insertRecordToDB(columnName, columnValue);
}
}
catch (IOException e) {
}
}
void insertRecordToDB(columnName, columnValue)
{
ContentValues values = new ContentValues();
values.put(columnName, columnaValue);
db.insert(DB_TABLE, null, values);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.