如何在 Android 中创建本地数据库?
How to create a Local Database in Android?
我在尝试创建数据库时遇到问题...
我想我在 android 中创建数据库时做错了什么。
我也在网上搜索但没有找到任何好的线索。
这是我的代码:
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")));
**
您应该阅读一些有关 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();
Insert 函数以及使用准备好的语句是更安全的数据插入方式。他们将采用转义字符串并将它们放入正确的格式。
我在尝试创建数据库时遇到问题... 我想我在 android 中创建数据库时做错了什么。 我也在网上搜索但没有找到任何好的线索。 这是我的代码:
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")));
**
您应该阅读一些有关 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();
Insert 函数以及使用准备好的语句是更安全的数据插入方式。他们将采用转义字符串并将它们放入正确的格式。