DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String DBNAME = "sample.sql";
public static final String DBLOCATION = null;/*"/data/data/com.example.final_view.database/databases/"*/
public static final int DATABASE_VERSION = 3;
public static final String TABLE_NAME = "sample";
public static final String S_BUSSTOP_ID="BUSSTOP_ID";
public static final String S_BUSSTOP_NAME="BUSSTOP_NAME";
public static final String S_NAME_E="NAME_E";
public static final String S_LONGITUDE="LONGITUDE";
public static final String S_LATITUDE="LATITUDE";
public static final String S_ARS_ID="ARS_ID";
public static final String S_NEXT_BUS="NEXT_BUS";
public static final String S_NUM="NUM";
public static final String S_STATION_LIST_ID="STATION_LIST_ID";
private Context mContext;
private SQLiteDatabase mDatabase;
public DatabaseHelper (Context context)
{
super(context, DBNAME, null, DATABASE_VERSION);
Log.d("Database Operations", "Database created");
this.mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL('CREATE TABLE' + TABLE_NAME + '('+S_BUSSTOP_ID + 'INTEGER'+S_BUSSTOP_NAME+'TEXT'+S_NAME_E+"TEXT"+S_ARS_ID+
'TEXT'+S_LATITUDE+'TEXT'+S_LONGITUDE+'TEXT'+S_NEXT_BUS+'TEXT'+S_NUM+'TEXT'+S_STATION_LIST_ID+"TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public void openDatabase()
{
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase != null && mDatabase.isOpen())
{
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
//db종료
public void closeDatabase()
{
if (mDatabase!=null)
{
mDatabase.close();
}
}
//Method 값입력
public List<Busstation> getListProduct()
{
Busstation busstation = null;
List<Busstation> busstationList = new ArrayList<>();
openDatabase();
Cursor cursor = mDatabase.rawQuery('SELECT * FROM sample',null);// 여기 부분에서 에러가 나오는 부분입니다. sample.sql table 이름과 일치하게 해도 오류가 뜹니다..... cursor.moveToFirst();
while (!cursor.isAfterLast())
{
busstation =new Busstation(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));
busstationList.add(busstation);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return busstationList;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView lvProduct;
private ListProductAdapter adapter;
private List<Busstation> mBusstationList;
private DatabaseHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Process in onCreate
lvProduct = (ListView)findViewById(R.id.listView_station);
mDBHelper = new DatabaseHelper(this);
//check exists database
File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
if (false == database.exists())
{
mDBHelper.getReadableDatabase();
//copy DB
if(copyDatabase(this))
{
Toast.makeText(this,"Copy database success", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(this,"Copy data error", Toast.LENGTH_SHORT).show();
return;
}
}
//GEt product list in db when db exists
mBusstationList = mDBHelper.getListProduct();
//Init adapter
adapter = new ListProductAdapter(this, mBusstationList);
//Set adapter for listview
lvProduct.setAdapter(adapter);
// note: need to use multithread for Large DB
}
//create method copy database
private boolean copyDatabase(Context context)
{
try
{
InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[]buff = new byte[1024];
int length = 0;
while((length=inputStream.read(buff))>0)
{
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w("MainActivity","DB copied");
return true;
}catch (Exception e)
{
e.printStackTrace();
return false;
}
}
}
mBusstationList = mDBHelper.getListProduct(); 이부분에서 오류가 뜹니다!!
************************오류 내용 입니다*****************************
错误:
07-22 06:45:56.230 19094-19094/com.example.final_view D/Database Operations: Database created
07-22 06:45:56.241 19094-19094/com.example.final_view E/SQLiteLog: (1) no such table: sample
07-22 06:45:56.241 19094-19094/com.example.final_view D/AndroidRuntime: Shutting down VM
07-22 06:45:56.246 19094-19094/com.example.final_view E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.final_view, PID: 19094
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.final_view/com.example.final_view.MainActivity}: android.database.sqlite.SQLiteException: no such table: sample (code 1): , while compiling: SELECT * FROM sample
答案 0 :(得分:4)
该表根本没有创建,因为您使用的表创建命令完全错误:
db.execSQL('CREATE TABLE' + TABLE_NAME + '('+S_BUSSTOP_ID + 'INTEGER'+S_BUSSTOP_NAME+'TEXT'+S_NAME_E+"TEXT"+S_ARS_ID+ 'TEXT'+S_LATITUDE+'TEXT'+S_LONGITUDE+'TEXT'+S_NEXT_BUS+'TEXT'+S_NUM+'TEXT'+S_STATION_LIST_ID+"TEXT);");
0 - '
必须为"
1 - 您错过空格以将列名与其数据类型分开
2 - 表名也必须分开
3 - 您在列定义之间错过了逗号。
这是正确的版本:
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + S_BUSSTOP_ID + " INTEGER, " + S_BUSSTOP_NAME + " TEXT, " + S_NAME_E + " TEXT, " + S_ARS_ID + " TEXT, " + S_LATITUDE + " TEXT, " + S_LONGITUDE + " TEXT, " + S_NEXT_BUS + " TEXT, " + S_NUM + " TEXT, " + S_STATION_LIST_ID + " TEXT)");
你也错过了这里的空间
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
必须是
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
这个
Cursor cursor = mDatabase.rawQuery('SELECT * FROM sample',null);
必须是
Cursor cursor = mDatabase.rawQuery("SELECT * FROM sample", null);