简体   繁体   中英

How to insert values using JAVA code in sqlite databse table?

Here is the database helper file which i used to make table in sqlite database for my application.

package net.learn2develop.listview;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.content.ContentValues;




    public class DBHelper extends SQLiteOpenHelper 
    {

        static String DB_NAME="AmericanDB.db";
        static int DB_VERSION=1;

        // TABLE 1
        static String TABLE_NAME="Hex_Bolts";
        static String ID=BaseColumns._ID;
        static String Basic_Dia="Basic Diameter";
        static String Actual_Dia="Actual Diameter";
        static String Face_Width="Face Width";
        static String Point_Width="Point Width";
        static String Head_Height="Head Height";

        public DBHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, DB_NAME,null,DB_VERSION);
            // TODO Auto-generated constructor stub
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
        +Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ "text, " 
                    +Point_Width+ "text, " +Head_Height+ "text)";
            db.execSQL(query);



    }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub

        }


        @Override
        public SQLiteDatabase getWritableDatabase() {
            // TODO Auto-generated method stub
            return super.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put("Basic Diameter", "0.2500");
            values.put("Actual Diameter", "0.260");
            values.put("Face Width", "0.438");
            values.put("Point Width", "0.505");
            values.put("Head Height", "0.188");
            db.insert(TABLE_NAME, null, values);
            db.close();
        }

}

It contains some errors as I don't know the method for inserting data values in the database so please refer this and answer as per my file. Thank you.

Change

String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
    +Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ "text, " 
                +Point_Width+ "text, " +Head_Height+ "text)";

to

String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
    +Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ " text, " 
                +Point_Width+ " text, " +Head_Height+ " text)";

You need to add spaces after Face_Width , Point_Width and Head_Height

Correct your Create Table query:

String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
+Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ " text, " 
            +Point_Width+ " text, " +Head_Height+ " text)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM