user14187680
user14187680

Reputation:

Create a Table in SQLite with the following fields

I have this method to create SQLite database, now am wondering code command that will create a table in the database called Medical_Information with column id as AUTOINCREMENT, INT,and then Time of data type DateTime,Dosage_description(VARCHAR255).

//Method to create database in slite
    public void createDatabase(){
       SQLiteDatabase myDB = this.openOrCreateDatabase("Dosage", MODE_PRIVATE, null);
    
    }
//Method to create table

Upvotes: 0

Views: 1331

Answers (1)

Toygur Ziya Kutlu
Toygur Ziya Kutlu

Reputation: 287

Syntax for creating table is:

CREATE TABLE tableName (columnName1 valueType1, columnName2 valuType2...);

Below syntax will create table like as you wanted.

private static final String CREATE_TABLE = "CREATE TABLE Medical_Information (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "Time DATETIME, " +
                "Dosage_description VARCHAR(255))";

You can also use SQLiteStudio for creating database and tables and you can use that database or you can create database and tables and you can copy syntax for creating table from SQLiteStudio.

EDIT:

Can you post for inserting sample data into table?

Either you can use SQL syntax or insert() method. Syntax for inserting data into table:

INSERT INTO tableName (column1, column2....) VALUES (value1, value2,...);

Using ContentValues with insert() method:

ContentValues values = new ContentValues();
values.put(columnName, value);
db.insert(tableName, columnHack, values);

Inserting data with SQL syntax:

String sql = "INSERT INTO Medical_Information (Time, Dosage_description) VALUES (datetime(), 'test')";
db.execSQL(sql);
db.close();

Note: datetime() is a time function of SQL so you can't use it with ContentValues (but you can use Java's time methods). Inserting data with insert() method:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ContentValues values = new ContentValues();
values.put("Time", sdf.format(new Date()));
values.put("Dosage_description", "test");

db.insert("Medical_Information", null, values);
db.close();

Upvotes: 1

Related Questions