「挿入」ボタンをクリックすると、次のエラーが見つかりました
Error Found :android.database.sqlite.SQLiteException: no such column : id (code 1) while compiling : select id,student_name, student_rollno FROM studentTable WHERE idrh
エラーが見つかりました:android.database.sqlite.SQLiteException:そのような列はありません:コンパイル中のid(コード1):id、student_name、student_rollno FROM studentTable WHERE idrhを選択します
注:rhは入力したIDです
コーディング
Dbhelper.java
package com.example.a;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DbActivity {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "student_name";
public static final String KEY_ROLLNO = "student_rollno";
private static final String DATABASE_NAME= "Studentdb";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "studentTable";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/* db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ( " +
KEY_ID + " TEXT PRIMARY KEY , " +
KEY_NAME + " TEXT NOT NULL , " +
KEY_ROLLNO + " TEXT NOT NULL );"
*/
db.execSQL("CREATE TABLE if not exists database_table (id TEXT PRIMARY KEY ,"
+ "student_name"
+ " TEXT ,"
+ "student_rollno"
+ " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DbActivity(Context c) {
ourContext = c;
}
public DbActivity open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public void createEntry(String id, String name, String rollno) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ID, id);
cv.put(KEY_NAME, name);
cv.put(KEY_ROLLNO, rollno);
ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
String result = "";
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null,null);
int id = c.getColumnIndex(KEY_ID);
int name = c.getColumnIndex(KEY_NAME);
int rollno = c.getColumnIndex(KEY_ROLLNO);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext());
{
result = result + c.getColumnName(id) + "" + c.getColumnName(name) + "" + c.getColumnName(rollno) + "/n";
}
return result;
}
public String getName(String s) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "" + s, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getRollno(String s) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "" + s, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
String rollno = c.getString(2);
return rollno;
}
return null;
}
public void updateEntry (String id1, String name, String rollno) throws SQLException {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_ID, id1);
cvUpdate.put(KEY_NAME, name);
cvUpdate.put(KEY_ROLLNO, rollno);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ID + "=" + id1 , null);
}
public void deleteEntry(String id) throws SQLException {
ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id , null);
}
}
MainActivity.java
package com.example.a;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DbActivity {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "student_name";
public static final String KEY_ROLLNO = "student_rollno";
private static final String DATABASE_NAME= "Studentdb";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "studentTable";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/* db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ( " +
KEY_ID + " TEXT PRIMARY KEY , " +
KEY_NAME + " TEXT NOT NULL , " +
KEY_ROLLNO + " TEXT NOT NULL );"
*/
db.execSQL("CREATE TABLE if not exists database_table (id TEXT PRIMARY KEY ,"
+ "student_name"
+ " TEXT ,"
+ "student_rollno"
+ " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DbActivity(Context c) {
ourContext = c;
}
public DbActivity open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public void createEntry(String id, String name, String rollno) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ID, id);
cv.put(KEY_NAME, name);
cv.put(KEY_ROLLNO, rollno);
ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
String result = "";
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null,null);
int id = c.getColumnIndex(KEY_ID);
int name = c.getColumnIndex(KEY_NAME);
int rollno = c.getColumnIndex(KEY_ROLLNO);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext());
{
result = result + c.getColumnName(id) + "" + c.getColumnName(name) + "" + c.getColumnName(rollno) + "/n";
}
return result;
}
public String getName(String s) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "" + s, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getRollno(String s) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID,KEY_NAME,KEY_ROLLNO};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "" + s, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
String rollno = c.getString(2);
return rollno;
}
return null;
}
public void updateEntry (String id1, String name, String rollno) throws SQLException {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_ID, id1);
cvUpdate.put(KEY_NAME, name);
cvUpdate.put(KEY_ROLLNO, rollno);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ID + "=" + id1 , null);
}
public void deleteEntry(String id) throws SQLException {
ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id , null);
}
}
}
activity_main.xml ファイル
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/etSQLName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Enter Name" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bSQLInsert"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/bSQLMain"
android:layout_marginLeft="17dp"
android:layout_marginTop="38dp"
android:text="Insert" />
<Button
android:id="@+id/bSQLUpdate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bSQLInsert"
android:layout_alignBottom="@+id/bSQLInsert"
android:layout_toRightOf="@+id/bSQLInsert"
android:text="update" />
<Button
android:id="@+id/bSQLView"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bSQLUpdate"
android:layout_alignBottom="@+id/bSQLUpdate"
android:layout_toRightOf="@+id/bSQLUpdate"
android:text="view" />
<Button
android:id="@+id/bSQLDelete"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/bSQLView"
android:layout_toRightOf="@+id/bSQLView"
android:text="delete" />
<Button
android:id="@+id/bSQLBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bSQLUpdate"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="back" />
<Button
android:id="@+id/bSQLMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etSQLRollno"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp"
android:text="Next" />
<EditText
android:id="@+id/etSQLRollno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etSQLName"
android:layout_below="@+id/etSQLName"
android:ems="10"
android:hint="Enter Rollno"
/>
<EditText
android:id="@+id/etSQLId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etSQLRollno"
android:layout_below="@+id/etSQLRollno"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="enter ID"
/>
2> 質問 - AVD または仮想デバイスによって既に作成されているテーブルを削除するにはどうすればよいですか