如何在Android中创建本地数据库?

时间:2016-01-12 06:29:09

标签: android sql database database-design rdbms

我在尝试创建数据库时遇到问题... 我想在android中创建DB时我做错了什么。 我也搜索过网络,但没有发现任何好的线索。 这是我的代码:

     package com.example.raqib.database;

        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.os.Bundle;
        import android.support.design.widget.FloatingActionButton;
        import android.support.design.widget.Snackbar;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.util.Log;
        import android.view.Gravity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;

        public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

            static EditText stName;
            static EditText stRollNo;
            static EditText stSem;
            Button enterDB;
            private Object context;


            // creating database



            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);


                getIntent();


                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();
                    }
                });
            }


            public void enterDetails(View view) {

                stName = (EditText) findViewById(R.id.stName);

                stRollNo = (EditText) findViewById(R.id.stRollNo);

                stSem = (EditText) findViewById(R.id.stSem);

                enterDB = (Button) findViewById(R.id.enterDB);
                enterDB.setOnClickListener(this);

            }

            public void onClick(View v){

                String st_Name = stName.getText().toString();
                int st_RollNo = Integer.parseInt(stRollNo.getText().toString());
                String st_sem = stSem.getText().toString();




               SQLiteDatabase db = openOrCreateDatabase("database", MODE_PRIVATE, null);
               db.execSQL("create table if not exists student (name varchar, roll_no int(4), semester varchar);");
            //   db.execSQL("insert into student( name , roll_no , semester )" + "values"+" ('"+ st_Name + "'," + st_RollNo + ",'" +st_sem + "');");


ContentValues values = new ContentValues();
values.put("name", st_Name); 
values.put("roll_no", st_RollNo); values.put("semester", st_sem); db.insert("student", null, values);



                Cursor cursor = db.rawQuery("select * from student", null);
                cursor.moveToFirst();
                Log.e("Name : ", cursor.getString(cursor.getColumnIndex("name")));

                Toast toast = Toast.makeText(MainActivity.this, " WOW! Successfully entered data in Database", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER,0, 0);
                toast.show();


               // student registeredData = new student(st_Name,st_RollNo,st_sem);
            }



        }

**我面临的错误是:它通过以下语句显示st_Name而不是其值:

Log.e("Name : ", cursor.getString(cursor.getColumnIndex("name")));

**

1 个答案:

答案 0 :(得分:3)

您应该阅读有关SQLiteOpenHelper的内容。这是在Android中使用SQLite数据库的更好方法。

但是对于您的情况,您忘记将文本值包装在'

db.execSQL("insert into student( name , roll_no , semester )" + " values "+
"('"+ st_Name + "'," + st_RollNo + ",'" +st_sem + "');");

避免此类问题的android方法是将值设置为插入ContentValues对象并使用数据库的插入函数:

ContentValues values = new ContentValues();
values.put("name", st_Name);
values.put("roll_no", st_RollNo);
values.put("semester", st_sem);
db.insert("student", null, values);

或者您可以使用预备语句:

SQLiteStatement stmt = db.compileStatement("insert into student( name , roll_no , semester ) values (?,?,?);");
stmt.bindString(1, st_Name);
stmt.bindInt(1, st_RollNo);
stmt.bindString(1, st_sem);
stmt.execute();

插入函数以及使用预准备语句是插入数据的更安全的方法。他们将采取逃避字符串并将其放入正确的格式。