Просто я делаю 2 databaseHelper в моем android проекте для создания таблицы, первая такая
package com.example.myapplication.adapter;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
public static final String TAG = "DBHelper";
// columns of the companies table
public static final String TABLE_COMPANIES = "companies";
public static final String COLUMN_COMPANY_ID = "_id";
public static final String COLUMN_COMPANY_NAME = "company_name";
// columns of the employees table
public static final String TABLE_EMPLOYEES = "employees";
public static final String COLUMN_EMPLOYE_ID = COLUMN_COMPANY_ID;
public static final String COLUMN_EMPLOYE_FIRST_NAME = "first_name";
public static final String COLUMN_EMPLOYE_SALARY = "salary";
public static final String COLUMN_EMPLOYE_COMPANY_ID = "company_id";
private static final String DATABASE_NAME = "companies.db";
private static final int DATABASE_VERSION = 1;
// SQL statement of the employees table creation
private static final String SQL_CREATE_TABLE_EMPLOYEES = "CREATE TABLE " + TABLE_EMPLOYEES + "("
+ COLUMN_EMPLOYE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_EMPLOYE_FIRST_NAME + " TEXT NOT NULL, "
+ COLUMN_EMPLOYE_SALARY + " REAL NOT NULL, "
+ COLUMN_EMPLOYE_COMPANY_ID + " INTEGER NOT NULL "
+");";
// SQL statement of the companies table creation
private static final String SQL_CREATE_TABLE_COMPANIES = "CREATE TABLE " + TABLE_COMPANIES + "("
+ COLUMN_COMPANY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_COMPANY_NAME + " TEXT NOT NULL "
+");";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(SQL_CREATE_TABLE_COMPANIES);
database.execSQL(SQL_CREATE_TABLE_EMPLOYEES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading the database from version " + oldVersion + " to "+ newVersion);
// clear all data
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMPANIES);
// recreate the tables
onCreate(db);
}
public DBHelper(Context context, String name, CursorFactory factory,int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
}
затем я делаю второй помощник db следующим образом
package com.example.myapplication.adapter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.myapplication.beratkucing;
import com.example.myapplication.listing;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String TAG = "DatabaseHelper";
public static final int DB_VERSION = 1;
public static final String DB_NAME = "yoi.db";
private SQLiteDatabase db;
Context context;
//list kucing
public static final String TABLE_CHART = "Berat";
public static final String COL_NAMAA = "NAMAA";
public static final String COL_BERAT = "WEIGHT";
public static final String COL_TANGGAL = "TANGGAL";
//list berat tag
public static final String TABLE_TAG = "tag";
public static final String COLUMN_TAG_ID = "_id";
public static final String COLUMN_TAG_NAME = "tag_name";
//list berat todo
public static final String TABLE_TODO = "todo";
public static final String COLUMN_TODO_ID = COLUMN_TAG_ID;
public static final String COLUMN_TODO_BERAT = "berat";
public static final String COLUMN_TODO_TANGGAL = "tanggal";
public static final String COLUMN_TODO_TAG_ID = "tag_id";
private static final String SQL_CREATE_TABLE_TODO;
private static final String SQL_CREATE_TABLE_TAG;
static {
SQL_CREATE_TABLE_TODO = "CREATE TABLEtodo(_id TEXT ,berat INTEGER NOT NULL,tanggal TEXT NOT FULL);";
SQL_CREATE_TABLE_TAG = "CREATE TABLEtag(_id INTEGER PRIMARY KEY AUTOINCREMENT, tag_name TEXT NOT NULL, );";
}
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public static void setConfig(beratkucing beratkucing) {
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(listing.SQL_CREATE);
db.execSQL(SQL_CREATE_TABLE_TAG);
db.execSQL(SQL_CREATE_TABLE_TODO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading the database from version " + oldVersion + "to"+ newVersion);
db.execSQL(listing.SQL_DELETE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAG);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
onCreate(db);
}
public void insertTransaction(String Nama, String Sertfikat, String DOB, String Jenis_Kelamin, String Warna_Kucing){
db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(listing.COL_NAMA, Nama);
values.put(listing.COL_SERTIFIKAT, Sertfikat);
values.put(listing.COL_DOB, DOB);
values.put(listing.COL_JENIS, Jenis_Kelamin);
values.put(listing.COL_WARNA, Warna_Kucing);
db.insert(listing.TABLE_NAMA,null, values);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_NAME, factory, DB_VERSION);
}
public List<listing> getTransactions(){
db = getReadableDatabase();
List<listing> transactions = new ArrayList<>();
Cursor cursor = db.rawQuery("select * from "+listing.TABLE_NAMA+
" order by "+listing._ID, null);
listing newTrans;
try{
while (cursor.moveToNext()){
newTrans = new listing(Long.toString(cursor.getLong(0)), cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5));
transactions.add(newTrans);
}
} finally {
cursor.close();
}
return transactions;
}
public void deleteTransaction(listing Kucing) {
db = getWritableDatabase();
db.delete(listing.TABLE_NAMA, listing._ID + " = ?",
new String[]{Kucing.getId()});
}
}
, но отображается ошибка «Вызвано: android .database.sqlite.SQLiteException: нет такого столбца: _id (код 1 SQLITE_ERROR): при компиляции: выберите * from Kucing order by _id ", когда я щелкнул свой список, получите showdata
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.adapter.DatabaseHelper;
import java.util.ArrayList;
public class listkucing extends AppCompatActivity {
private ArrayList<listing> transList = new ArrayList<>();
ArrayAdapter adapter;
ListView transactionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listkucing);
transactionList = (ListView) findViewById(R.id.list_kucing);
DatabaseHelper dbHelper = new DatabaseHelper(this);
transList = (ArrayList<listing>) dbHelper.getTransactions();
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, transList);
transactionList.setAdapter(adapter);
transactionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getBaseContext(),
third.class);
intent.putExtra("Kucing.detail", transList.get(position));
startActivity(intent);
}
});
}
}
package com.example.myapplication;
import android.provider.BaseColumns;
import java.io.Serializable;
public class listing implements Serializable, BaseColumns {
public static final String TABLE_NAMA = "Kucing";
public static final String COL_ID = "_id";
public static final String COL_NAMA = "Nama";
public static final String COL_SERTIFIKAT = "Sertifikat";
public static final String COL_DOB = "DOB";
public static final String COL_JENIS = "Jenis_Kelamin";
public static final String COL_WARNA = "Warna_Kucing";
public static final String SQL_CREATE = "CREATE TABLE " + TABLE_NAMA + "("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_NAMA + " TEXT NOT NULL, "
+ COL_SERTIFIKAT + " TEXT NOT NULL, "
+ COL_DOB + " TEXT NOT NULL, "
+ COL_JENIS + " TEXT NOT NULL, "
+ COL_WARNA + " TEXT NOT NULL "+");";
public static final String SQL_DELETE = "drop table if exists Kucing";
private String NAMAA;
private int WEIGHT;
private String TANGGAL;
private String id;
private String Nama;
private String Sertifikat;
private String DOB;
private String Jenis_Kelamin;
private String Warna_Kucing;
public listing(String id, String Nama, String Sertifikat, String DOB, String Jenis_Kelamin, String Warna_Kucing) {
this.id = id;
this.Nama = Nama;
this.Sertifikat = Sertifikat;
this.DOB = DOB;
this.Jenis_Kelamin = Jenis_Kelamin;
this.Warna_Kucing = Warna_Kucing;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNama() {
return Nama;
}
public void setNama(String Nama) {
this.Nama = Nama;
}
public String getSertifikat() {
return Sertifikat;
}
public void setSertifikat(String Serfikat) {
this.Sertifikat = Serfikat;
}
public String getDOB() {
return DOB;
}
public void setDOB(String DOB) {
this.DOB = DOB;
}
public String getJenis_Kelamin() {
return Jenis_Kelamin;
}
public void setJenis_Kelamin(String Jenis_Kelamin){
this.Jenis_Kelamin = Jenis_Kelamin;
}
public String getWarna_Kucing() {
return Warna_Kucing;
}
public void setWarna_Kucing(String Warna_Kucing) {
this.Warna_Kucing = Warna_Kucing;
}
public String getNAMAA() {
return NAMAA;
}
public void setNAMAA(String NAMAA) {
this.NAMAA = NAMAA;
}
public int getWEIGHT() {
return WEIGHT;
}
public void setWEIGHT(int WEIGHT) {
this.WEIGHT = WEIGHT;
}
public String getTANGGAL() {
return TANGGAL;
}
public void setTANGGAL(String TANGGAL) {
this.TANGGAL = TANGGAL;
}
@Override
public String toString() {
return this.Nama;
}
}